Skip to content

Instantly share code, notes, and snippets.

@almozavr
Created September 25, 2014 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almozavr/d8dd7598691542bbf9af to your computer and use it in GitHub Desktop.
Save almozavr/d8dd7598691542bbf9af to your computer and use it in GitHub Desktop.
private static WeakReference<RenderScript> rsRef = new WeakReference<>(null);
private static WeakReference<ScriptIntrinsicBlur> blurScriptRef = new WeakReference<>(null);
/**
* Use {@link RenderScript} to blur bitmap.
*
* @param bitmap to be blurred
* @param radius of blur
* @throws RSRuntimeException when some .so libs are not available (e.g. on Genymotion emulator)
*/
public static synchronized void applyBlur(Context context, Bitmap bitmap, Float radius) throws RSRuntimeException {
RenderScript rs = rsRef.get();
if (rs == null) {
rs = RenderScript.create(context.getApplicationContext());
rsRef = new WeakReference<>(rs);
}
//this will blur the bitmap with a provided radius and save it in bitmap
final Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT | Allocation.USAGE_SHARED);
final Allocation output = Allocation.createTyped(rs, input.getType());
ScriptIntrinsicBlur blurScript = blurScriptRef.get();
if (blurScript == null) {
blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
blurScriptRef = new WeakReference<>(blurScript);
}
blurScript.setRadius(radius);
blurScript.setInput(input);
blurScript.forEach(output);
output.copyTo(bitmap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment