如何实现Android 布局背景模糊化处理
2016-04-13 09:43
489 查看
在模仿 IOS 密码输入页面的时候发现其背景有模糊处理,于是了解了一下并记录下来,以便使用.在Android 中具体实现方法如下查考 http://www.cnblogs.com/lipeil/p/3997992.html[code]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 来实现,而且这样可以缩短模糊化得时间
@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高级模糊技术
- Android ImageView的scaleType属性与adjustViewBounds属性
- Android Xfermode 实战 实现圆形、圆角图片
- Android Volley完全解析(四),带你从源码的角度理解Volley
- Android Volley完全解析(一),初识Volley的基本用法【转】
- Android-ViewPagerIndicator框架使用——CirclePageIndicator
- Android有效解决加载大图片时内存溢出的问题
- Android 实现图片的圆角 (转)
- Android中shape的使用
- 【转】Android 实现ListView的滑动删除效果
- 最新Android & iOS设计尺寸规范
- Android Studio 配置
- 根据年和月计算这个月有多少天的算法
- Android使用ViewPager实现左右循环滑动及轮播效果
- android进程和线程
- android项目大全,总有你所需的(二)
- Android开发中,那些让你相见恨晚的方法、类或接口
- 通用的RecyclerView Adapter适配器
- Android减少布局层次--有关Activity根视图DecorView的思考
- Android自定义控件简单实现ratingbar效果