android 布局背景模糊化处理

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

查考 http://www.cnblogs.com/lipeil/p/3997992.html

Java代码   收藏代码
  1. private void applyBlur() {   
  2.         
  3.     // 获取壁纸管理器    
  4.     WallpaperManager wallpaperManager = WallpaperManager.getInstance(this.getContext());    
  5.     // 获取当前壁纸    
  6.     Drawable wallpaperDrawable = wallpaperManager.getDrawable();    
  7.     // 将Drawable,转成Bitmap    
  8.     Bitmap bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();    
  9.       
  10.     blur(bmp);   
  11. }   

 

下面之所以要进行small 和big的处理,是因为仅仅靠ScriptIntrinsicBlur  来处理模式,不能到达更模式的效果,如果需要加深模式效果就需要先把背景图片缩小,在处理完之后再放大.这个可以使用Matrix 来实现,而且这样可以缩短模糊化得时间

Java代码   收藏代码
  1. @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)   
  2. private void blur(Bitmap bkg) {   
  3.     long startMs = System.currentTimeMillis();   
  4.     float radius = 20;   
  5.   
  6.     bkg = small(bkg);  
  7.     Bitmap bitmap = bkg.copy(bkg.getConfig(), true);  
  8.   
  9.     final RenderScript rs = RenderScript.create(this.getContext());  
  10.     final Allocation input = Allocation.createFromBitmap(rs, bkg, Allocation.MipmapControl.MIPMAP_NONE,  
  11.             Allocation.USAGE_SCRIPT);  
  12.     final Allocation output = Allocation.createTyped(rs, input.getType());  
  13.     final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));  
  14.     script.setRadius(radius);  
  15.     script.setInput(input);  
  16.     script.forEach(output);  
  17.     output.copyTo(bitmap);  
  18.   
  19.     bitmap = big(bitmap);  
  20.     setBackground(new BitmapDrawable(getResources(), bitmap));   
  21.     rs.destroy();   
  22.     Log.d("zhangle","blur take away:" + (System.currentTimeMillis() - startMs )+ "ms");   
  23. }   
  24.   
  25. private static Bitmap big(Bitmap bitmap) {  
  26.       Matrix matrix = new Matrix();   
  27.       matrix.postScale(4f,4f); //长和宽放大缩小的比例  
  28.       Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);  
  29.       return resizeBmp;  
  30.  }  
  31.   
  32.  private static Bitmap small(Bitmap bitmap) {  
  33.       Matrix matrix = new Matrix();   
  34.       matrix.postScale(0.25f,0.25f); //长和宽放大缩小的比例  
  35.       Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);  
  36.       return resizeBmp;  
  37. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Android 中实现背景模糊,可以使用 RenderScript 或者使用 AndroidX 中的 androidx.palette 库中的 Palette API。 使用 RenderScript 实现背景模糊的步骤如下: 1. 首先在 build.gradle 文件中添加 RenderScript 的支持: ``` android { ... defaultConfig { ... renderscriptTargetApi 19 renderscriptSupportModeEnabled true } } ``` 2. 创建一个 RenderScript 内核: ``` #pragma version(1) #pragma rs java_package_name(com.example.myapp) rs_allocation inputImage; rs_allocation outputImage; int radius; void root(const uchar4* in, uchar4* out, uint32_t x, uint32_t y) { float4 sum = 0; int count = 0; for (int i = -radius; i <= radius; i++) { for (int j = -radius; j <= radius; j++) { float4 color = rsUnpackColor8888(rsGetElementAt_uchar4(inputImage, x + i, y + j)); sum += color; count++; } } out->xyz = sum.xyz / count; out->w = 255; } ``` 3. 调用 RenderScript 内核: ``` final RenderScript rs = RenderScript.create(context); final Allocation input = Allocation.createFromBitmap(rs, bitmap); 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); ``` 使用 Palette API 实现背景模糊的步骤如下: 1. 首先在 build.gradle 文件中添加 androidx.palette 库的依赖: ``` dependencies { implementation 'androidx.palette:palette:1.0.0' } ``` 2. 加载图片并提取颜色: ``` val bitmap = BitmapFactory.decodeResource(resources, R.drawable.my_image) val palette = Palette.from(bitmap).generate() val color = palette.getMutedColor(ContextCompat.getColor(this, R.color.default_background)) ``` 3. 将颜色应用到背景上: ``` val drawable = GradientDrawable() drawable.setColor(color) window.decorView.background = drawable ``` 以上是两种实现 Android 背景模糊的方法,具体选择哪种方法取决于你的需求和代码实现的复杂程度。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值