Android中实现布局背景模糊化处理的方法

所属分类: 软件编程 / Android 阅读数: 1404
收藏 0 赞 0 分享

在模仿 IOS 密码输入页面的时候发现其背景有模糊处理,于是了解了一下并记录下来,以便使用.在Android 中具体实现方法如下
查考 https://www.jb51.net/article/64781.htm

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开发之Json文件的读写的示例代码

这篇文章主要介绍了android开发之Json文件的读写的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android7.0指纹服务FingerprintService实例介绍

这篇文章主要介绍了Android7.0指纹服务FingerprintService介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Android JNI处理图片实现黑白滤镜的方法

这篇文章主要介绍了Android JNI处理图片实现黑白滤镜的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android引入OpenCV的示例

本篇文章主要介绍了Android引入OpenCV的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android Zip解压缩工具类分享

这篇文章主要为大家详细介绍了Android Zip解压缩工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Interval

这篇文章主要为大家详细介绍了Android RxJava创建操作符Interval的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

5分钟快速实现Android爆炸破碎酷炫动画特效的示例

本篇文章主要介绍了5分钟快速实现Android爆炸破碎酷炫动效的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android 指纹功能实例代码

本文通过一个demo给大家介绍了android指纹功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现倒计时CountDownTimer使用详解

这篇文章主要为大家详细介绍了Android实现倒计时CountDownTimer的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Timer的方法

这篇文章主要为大家详细介绍了Android RxJava创建操作符Timer的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多