Skip to content

Instantly share code, notes, and snippets.

@martyglaubitz
Last active September 3, 2017 13:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martyglaubitz/19acfaf5a2b4326cc7af to your computer and use it in GitHub Desktop.
Save martyglaubitz/19acfaf5a2b4326cc7af to your computer and use it in GitHub Desktop.
A gaussian blur transformation for picasso, using renderscript.
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicBlur;
import com.squareup.picasso.Transformation;
public class GaussianBlurTransformation implements Transformation {
private final RenderScript _renderScript;
private final ScriptIntrinsicBlur _scriptIntrinsicBlur;
/**
* Creates a new GaussianBlurTransformation.
*
* Uses a default blur radius of 25f.
*
* @param context The context to create the RenderScript instance from.
*/
public GaussianBlurTransformation(final Context context) {
this(context, 25f);
}
/**
* Creates a new GaussianBlurTransformation.
*
* @param context The context to create the RenderScript instance from.
* @param radius The blur radius.
*/
public GaussianBlurTransformation(final Context context, final float radius) {
_renderScript = RenderScript.create(context);
_scriptIntrinsicBlur = ScriptIntrinsicBlur.create(_renderScript, Element.U8_4(_renderScript));
_scriptIntrinsicBlur.setRadius(radius);
}
@Override public Bitmap transform(final Bitmap source) {
final Bitmap result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
final Allocation allocationInput = Allocation.createFromBitmap(_renderScript, source);
final Allocation allocationOutput = Allocation.createFromBitmap(_renderScript, result);
// perform the transformation
_scriptIntrinsicBlur.setInput(allocationInput);
_scriptIntrinsicBlur.forEach(allocationOutput);
// copy the final bitmap created by the output Allocation to the outBitmap
allocationOutput.copyTo(result);
source.recycle();
_scriptIntrinsicBlur.destroy();
return result;
}
// allow picasso to cache the result of this trnasformation
@Override public String key() {
return "gauss-blur";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment