package com.marshalchen.common.uimodule.androidviewhover.tools; import android.graphics.Bitmap; import android.content.Context; import android.renderscript.Allocation; import android.renderscript.Element; import android.renderscript.RenderScript; import android.renderscript.ScriptIntrinsicBlur; public class Blur { private static final int DEFAULT_BLUR_RADIUS = 10; public static Bitmap apply(Context context, Bitmap sentBitmap) { return apply(context, sentBitmap, DEFAULT_BLUR_RADIUS); } 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; } }