Skip to content

Instantly share code, notes, and snippets.

@austynmahoney
Created April 10, 2014 20:30
Show Gist options
  • Save austynmahoney/10420197 to your computer and use it in GitHub Desktop.
Save austynmahoney/10420197 to your computer and use it in GitHub Desktop.
RenderScriptUtil
public class RenderScriptUtil {
private static final float DEFAULT_BLUR_RADIUS = 25f;
private RenderScriptUtil() {
}
public static BitmapDrawable blurImage(Context context, int resource) {
return blurImage(context, resource, DEFAULT_BLUR_RADIUS);
}
public static BitmapDrawable blurImage(Context context, int resource, float radius) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap inputBitmap = BitmapFactory.decodeResource(context.getResources(), resource, options);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap.getWidth(), inputBitmap.getHeight(), inputBitmap.getConfig());
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(radius);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
inputBitmap.recycle();
return new BitmapDrawable(context.getResources(), outputBitmap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment