当前位置: 移动技术网 > IT编程>移动开发>Android > Android中实现布局背景模糊化处理的方法

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

问 孟楠,中国陆军装备,汉朝皇帝刘贺

在模仿 ios 密码输入页面的时候发现其背景有模糊处理,于是了解了一下并记录下来,以便使用.在android 中具体实现方法如下
查考

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; 
} 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网