Skip to content

Instantly share code, notes, and snippets.

@SangsooNam
Created December 23, 2018 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SangsooNam/6c01f2932daf98df30f796d0de141444 to your computer and use it in GitHub Desktop.
Save SangsooNam/6c01f2932daf98df30f796d0de141444 to your computer and use it in GitHub Desktop.
Picasso BlurTransformation
package io.github.sangsoonam;
import android.content.Context;
import android.graphics.Bitmap;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import com.squareup.picasso.Transformation;
public class BlurTransformation implements Transformation {
private Context context;
public BlurTransformation(Context context) {
this.context = context;
}
@Override
public Bitmap transform(Bitmap source) {
Bitmap result = blurImage(source);
// You need to recycle. Otherwise, Picasso will throw an exception.
source.recycle();
return result;
}
@Override
public String key() {
// This is the key used for caching.
return "BlurTransformation";
}
private Bitmap blurImage(Bitmap source) {
Bitmap outBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation allIn = Allocation.createFromBitmap(rs, source);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
blurScript.setRadius(25.f);
blurScript.setInput(allIn);
blurScript.forEach(allOut);
allOut.copyTo(outBitmap);
rs.destroy();
allIn.destroy();
return outBitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment