Created
September 25, 2014 11:00
-
-
Save almozavr/d8dd7598691542bbf9af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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