Created
May 29, 2015 14:25
-
-
Save HugoGresse/fb0b33c27537202df13c to your computer and use it in GitHub Desktop.
RenderScript Blur with fallback
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
/** | |
* Blur the given bitmap and convert it to BitmapDrawable | |
* @param context the application context to use getRessources | |
* @param bitmapOriginal the bitmap you want to blur | |
* @param radius the blur radius (amount of blur) | |
* @return blured BitmapDrawable | |
*/ | |
public static Bitmap blur(Context context, Bitmap bitmapOriginal, int radius){ | |
try { | |
// define this only once if blurring multiple times | |
RenderScript rs = RenderScript.create(context); | |
//this will blur the bitmapOriginal with a radius of 8 and save it in bitmapOriginal | |
final Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal); //use this constructor for best performance, because it uses USAGE_SHARED mode which reuses memory | |
final Allocation output = Allocation.createTyped(rs, input.getType()); | |
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); | |
script.setRadius((float)radius); | |
script.setInput(input); | |
script.forEach(output); | |
output.copyTo(bitmapOriginal); | |
return bitmapOriginal; | |
} catch (NoClassDefFoundError e){ | |
TeadsLog.e("Teads ViewUtils", "You should register Android Support lib v8 with RenderScript."); | |
bitmapOriginal = Bitmap.createScaledBitmap( | |
bitmapOriginal, | |
bitmapOriginal.getWidth()/32, | |
bitmapOriginal.getHeight()/32, false); | |
return bitmapOriginal; | |
} catch(RSRuntimeException e){ | |
TeadsLog.i("Teads ViewUtils", "Unable to load RSSupport, proably no RenderScript on this device"); | |
bitmapOriginal = Bitmap.createScaledBitmap( | |
bitmapOriginal, | |
bitmapOriginal.getWidth()/32, | |
bitmapOriginal.getHeight()/32, false); | |
return bitmapOriginal; | |
} catch (Exception e){ | |
// We really don't want to have a crash on this | |
bitmapOriginal = Bitmap.createScaledBitmap( | |
bitmapOriginal, | |
bitmapOriginal.getWidth()/32, | |
bitmapOriginal.getHeight()/32, false); | |
return bitmapOriginal; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment