模糊背景处理--当前界面背景
2015-08-24 16:20
656 查看
获取手机墙纸或当前activity背景,做模糊处理,此方法有些耗时!
rootView为当前要设置的布局:
/**
* 以下为获取模糊背景代码
*/
private
void applyBlur() {
// 获取壁纸管理器
// WallpaperManager wallpaperManager = WallpaperManager.getInstance(this.getContext());
// // 获取当前壁纸
// Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// // 将Drawable,转成Bitmap
// Bitmap
bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();
blur(myShot((Activity)context));
}
/**
* 获取当前显示的activity背景
* @param activity
* @return
*/
public Bitmap myShot(Activity activity) {
// 获取windows中最顶层的view
View view = activity.getWindow().getDecorView();
view.buildDrawingCache();
// 获取状态栏高度
// Rect
rect = new
Rect();
// view.getWindowVisibleDisplayFrame(rect);
// int statusBarHeights = rect.top;
Display display = activity.getWindowManager().getDefaultDisplay();
// 获取屏幕宽和高
int widths = display.getWidth();
int heights = display.getHeight();
// 允许当前窗口保存缓存信息
view.setDrawingCacheEnabled(true);
// 去掉状态栏
// Bitmap
bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,statusBarHeights, widths, heights – statusBarHeights);
Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,0, widths, heights);
// 销毁缓存信息
view.destroyDrawingCache();
return bmp;
}
private
void blur(Bitmap bkg) {
long startMs = System.currentTimeMillis();
float radius = 20;
bkg = small(bkg);
Bitmap bitmap = bkg.copy(bkg.getConfig(),
true);
final RenderScript rs = RenderScript.create(this.getContext());
final Allocation input = Allocation.createFromBitmap(rs, bkg, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
bitmap = big(bitmap);
rootView.setBackground(new BitmapDrawable(context.getResources(), bitmap));
rs.destroy();
LddLogUtils.d("jack.log","blur take away:" + (System.currentTimeMillis() - startMs )+
"ms");
}
private
static Bitmap big(Bitmap bitmap) {
Matrix matrix =
new Matrix();
matrix.postScale(4f,4f);
//长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
}
private
static Bitmap small(Bitmap bitmap) {
Matrix matrix =
new Matrix();
matrix.postScale(0.25f,0.25f);
//长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
}
rootView为当前要设置的布局:
/**
* 以下为获取模糊背景代码
*/
private
void applyBlur() {
// 获取壁纸管理器
// WallpaperManager wallpaperManager = WallpaperManager.getInstance(this.getContext());
// // 获取当前壁纸
// Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// // 将Drawable,转成Bitmap
// Bitmap
bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();
blur(myShot((Activity)context));
}
/**
* 获取当前显示的activity背景
* @param activity
* @return
*/
public Bitmap myShot(Activity activity) {
// 获取windows中最顶层的view
View view = activity.getWindow().getDecorView();
view.buildDrawingCache();
// 获取状态栏高度
// Rect
rect = new
Rect();
// view.getWindowVisibleDisplayFrame(rect);
// int statusBarHeights = rect.top;
Display display = activity.getWindowManager().getDefaultDisplay();
// 获取屏幕宽和高
int widths = display.getWidth();
int heights = display.getHeight();
// 允许当前窗口保存缓存信息
view.setDrawingCacheEnabled(true);
// 去掉状态栏
// Bitmap
bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,statusBarHeights, widths, heights – statusBarHeights);
Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,0, widths, heights);
// 销毁缓存信息
view.destroyDrawingCache();
return bmp;
}
private
void blur(Bitmap bkg) {
long startMs = System.currentTimeMillis();
float radius = 20;
bkg = small(bkg);
Bitmap bitmap = bkg.copy(bkg.getConfig(),
true);
final RenderScript rs = RenderScript.create(this.getContext());
final Allocation input = Allocation.createFromBitmap(rs, bkg, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
bitmap = big(bitmap);
rootView.setBackground(new BitmapDrawable(context.getResources(), bitmap));
rs.destroy();
LddLogUtils.d("jack.log","blur take away:" + (System.currentTimeMillis() - startMs )+
"ms");
}
private
static Bitmap big(Bitmap bitmap) {
Matrix matrix =
new Matrix();
matrix.postScale(4f,4f);
//长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
}
private
static Bitmap small(Bitmap bitmap) {
Matrix matrix =
new Matrix();
matrix.postScale(0.25f,0.25f);
//长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories