实现在当前页面点击弹出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。