Created
April 10, 2014 20:30
-
-
Save austynmahoney/10420197 to your computer and use it in GitHub Desktop.
RenderScriptUtil
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
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