您的位置:首页 > 移动开发 > Android开发

Android 使用RenderScript 实现毛玻璃(模糊)

2017-12-05 16:24 197 查看
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方法等实现效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: