Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Basic BlurTransformer for Square's Picasso
import android.content.Context;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import com.squareup.picasso.Transformation;
public class BlurTransform implements Transformation {
RenderScript rs;
public BlurTransform(Context context) {
rs = RenderScript.create(context);
public Bitmap transform(Bitmap bitmap) {
// Create another bitmap that will hold the results of the filter.
Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);
// Allocate memory for Renderscript to work with
Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
Allocation output = Allocation.createTyped(rs, input.getType());
// Load up an instance of the specific script that we want to use.
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// Set the blur radius
// Start the ScriptIntrinisicBlur
// Copy the output to the blurred bitmap
return blurredBitmap;
public String key() {
return "blur";
Copy link

tadfisher commented Jan 9, 2014

You can make this a LOT faster by setting Allocation.USAGE_SHARED and removing the bitmap.recycle() call.

Copy link

akhy commented Apr 7, 2014

Just a note, most methods in ScriptIntrinsicBlur used in this snippet require API level 17.

Copy link

ryanbateman commented May 2, 2014

Thanks, Tad - noted and updated.

Copy link

rajuashok commented Oct 15, 2014

Note that if you don't call bitmap.recycle than the following call will fail:

.resize(width, height)
.transform(new BlurTransform(getActivity()))

with the following error: "Transformation blur mutated input Bitmap but failed to recycle the original."

So in this case I've re-added the .recycle() call.

Copy link

rajuashok commented Oct 23, 2014

Hmm, so I realize that be re-adding the recycle() call my image is not getting cached. So if I don't want to recycle I get the error mentioned above ("Transformation blur mutated input Bitmap but failed to recycle the original.").

Any idea how I can avoid this? Or is there no way to resize and blur an image at the same time?

Copy link

tokudu commented Oct 23, 2014

Quick note: you shouldn't creating a new RS instance in the constructor, which gets called every time - even when the blurred image is being loaded from memory. Instead, store a WeakReference<Context> and create RenderScript inside of the transform method.

Copy link

dp-singh commented Nov 6, 2014

It requires api level 17

Copy link

TommyVisic commented Nov 4, 2015

Thanks for posting this up.

I had to change one line to get things working in my case:

val blurredBitmap = Bitmap.createBitmap(bitmap)


val blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)

Problem was that BitMap.createBitmap() doesn't always create a copy - sometimes it reuses the bitmap. Looks like this was confusing Picasso's error checking in terms of whether or not the Transformation object correctly handled recycling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment