SoFunction
Updated on 2025-04-10

Methods for implementing layout background blurring in Android

When imitating the IOS password input page, I found that the background was blurred, so I learned about it and recorded it for use. The specific implementation method in Android is as follows
examinehttps:///article/

private void applyBlur() {  
    
  // Get the wallpaper manager  WallpaperManager wallpaperManager = (());  
  // Get the current wallpaper  Drawable wallpaperDrawable = ();  
  // Convert Drawable to Bitmap  Bitmap bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();  
   
  blur(bmp);  
}  

The reason for small and big processing is that the mode is only processed by ScriptIntrinsicBlur, and the more mode effects cannot be achieved. If you need to deepen the mode effect, you need to reduce the background image first and then zoom in after processing. This can be achieved using Matrix, and this can shorten the blurring time.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)  
private void blur(Bitmap bkg) {  
  long startMs = ();  
  float radius = 20;  
 
  bkg = small(bkg); 
  Bitmap bitmap = ((), true); 
 
  final RenderScript rs = (()); 
  final Allocation input = (rs, bkg, .MIPMAP_NONE, 
      Allocation.USAGE_SCRIPT); 
  final Allocation output = (rs, ()); 
  final ScriptIntrinsicBlur script = (rs, Element.U8_4(rs)); 
  (radius); 
  (input); 
  (output); 
  (bitmap); 
 
  bitmap = big(bitmap); 
  setBackground(new BitmapDrawable(getResources(), bitmap));  
  ();  
  ("zhangle","blur take away:" + (() - startMs )+ "ms");  
}  
 
private static Bitmap big(Bitmap bitmap) { 
   Matrix matrix = new Matrix();  
   (4f,4f); //The ratio of length and width enlargement and reduction   Bitmap resizeBmp = (bitmap,0,0,(),(),matrix,true); 
   return resizeBmp; 
 } 
 
 private static Bitmap small(Bitmap bitmap) { 
   Matrix matrix = new Matrix();  
   (0.25f,0.25f); //The ratio of length and width enlargement and reduction   Bitmap resizeBmp = (bitmap,0,0,(),(),matrix,true); 
   return resizeBmp; 
}