build.gradle 配置:
defaultConfig { renderscriptTargetApi 19 renderscriptSupportModeEnabled true }
获取当前view 截图
public static Bitmap getViewBitmap(View view) { if(view.getWidth() == 0 || view.getHeight() == 0) return null; Bitmap b = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); view.draw(c); return b; }
工具截图获取毛玻璃图片:
public static Bitmap apply(Context context, Bitmap sentBitmap, int radius) {
final Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, 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);
sentBitmap.recycle();
rs.destroy();
input.destroy();
output.destroy();
script.destroy();
return bitmap;
}
- 得到的模糊图片即View的高斯模糊效果图。可以通过addView方法或者setImageBitmap方法等实现效果