android 布局背景模糊化处理
2015-08-31 05:06
441 查看
在模仿 IOS 密码输入页面的时候发现其背景有模糊处理,于是了解了一下并记录下来,以便使用.在Android 中具体实现方法如下
查考 http://www.cnblogs.com/lipeil/p/3997992.html
Java代码
private void applyBlur() {
// 获取壁纸管理器
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this.getContext());
// 获取当前壁纸
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// 将Drawable,转成Bitmap
Bitmap bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();
blur(bmp);
}
下面之所以要进行small 和big的处理,是因为仅仅靠ScriptIntrinsicBlur 来处理模式,不能到达更模式的效果,如果需要加深模式效果就需要先把背景图片缩小,在处理完之后再放大.这个可以使用Matrix 来实现,而且这样可以缩短模糊化得时间
Java代码
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
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);
setBackground(new BitmapDrawable(getResources(), bitmap));
rs.destroy();
Log.d("zhangle","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;
}
查考 http://www.cnblogs.com/lipeil/p/3997992.html
Java代码
private void applyBlur() {
// 获取壁纸管理器
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this.getContext());
// 获取当前壁纸
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// 将Drawable,转成Bitmap
Bitmap bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();
blur(bmp);
}
下面之所以要进行small 和big的处理,是因为仅仅靠ScriptIntrinsicBlur 来处理模式,不能到达更模式的效果,如果需要加深模式效果就需要先把背景图片缩小,在处理完之后再放大.这个可以使用Matrix 来实现,而且这样可以缩短模糊化得时间
Java代码
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
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);
setBackground(new BitmapDrawable(getResources(), bitmap));
rs.destroy();
Log.d("zhangle","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;
}
相关文章推荐
- Android插件实例——360 DroidPlugin详解
- [Android] createTrack_l
- 一个优秀的Android应用从建项目开始
- Android-Listview(下拉刷新,底部加载更多,电影列表实例)
- QQ音乐/酷狗音乐锁屏控制实现原理
- Android Studio 笔记之SexyEditor,设置背景
- Android CheckBox
- Android权限相关
- CollapsingAvatarToolbar 头像随ListView滚动缩回到ActionBar特
- Material-Movies-master 影视软件封面页面呈现
- AppManager-for-Android App应用管理
- ImageView实现照片墙效果
- 安卓的优缺点
- 安卓常用开发命令
- android 还原短信
- android Content Provider
- android获取SD卡状态
- android如何对应用进行单元测试
- Android下创建一个sqlite数据库
- android MD5加密