毛玻璃效果

实现在当前页面点击弹出popupwindow,其背景为毛玻璃效果。
(注:模糊纯白或纯黑图片无效果)
在这里插入图片描述
实现步骤:
1、获取当前页面截图。
2、压缩截图(否则会oom)。
3、对压缩后的图进行模糊处理。

SoftReference<Bitmap> screenShot = null;//为避免oom
SoftReference<Bitmap> blurBitmapBg = null;
 		// 获取屏幕截图
        View dView = getWindow().getDecorView();
        dView.setDrawingCacheEnabled(true);
        dView.buildDrawingCache();
        screenShot = new SoftReference<Bitmap>(dView.getDrawingCache());
					 //质量压缩
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    screenShot.get().compress(Bitmap.CompressFormat.JPEG, (int) (quality * 100), baos);
                    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
                    Bitmap newBitMap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片

                    // 尺寸大小压缩
                    Matrix matrix = new Matrix();
                    matrix.setScale(0.6f, 0.6f);
                    newBitMap = Bitmap.createBitmap(newBitMap, 0, 0, newBitMap.getWidth(), newBitMap.getHeight(), matrix, true);
 blurBitmapBg = new SoftReference<Bitmap>(BlurringViewUtils.doBlur(newBitMap, 50, true));//返回模糊后的bitmap,直接设置到imageview即可。
 //在onDestroy方法里,清除bitmap数据。
 if (blurBitmapBg != null) {
            blurBitmapBg.clear();
        }
        if (screenShot != null) {
            screenShot.clear();
        }

模糊图片的算法有很多,可自行挑选。我用的工具类是来自https://blog.csdn.net/xulike1990/article/details/60139908

注意事项:
压缩图片和模糊图片都是耗时操作,所以要放到子线程中执行;
bitmap格式的图片建议使用软引用,避免回收不及时,出现oom。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值