Skip to content

Instantly share code, notes, and snippets.

@chrisjenx
Forked from ryanbateman/gist:6667995
Last active August 29, 2015 14:22
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 chrisjenx/4a92a602c4dc323e1da1 to your computer and use it in GitHub Desktop.
Save chrisjenx/4a92a602c4dc323e1da1 to your computer and use it in GitHub Desktop.
BlurTransformer for Picasso using renderscript lib.
package com.loveflutter.ui.support;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
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;
import java.lang.ref.WeakReference;
public class BlurTransform implements Transformation {
public static BlurTransform get(@NonNull Context context) {
return new BlurTransform(context);
}
private final WeakReference<Context> contextReference;
private BlurTransform(Context context) {
super();
contextReference = new WeakReference<>(context);
}
@Override
public Bitmap transform(Bitmap source) {
final RenderScript rs = RenderScript.create(contextReference.get());
// Create another source that will hold the results of the filter.
final Bitmap blurredBitmap = source.copy(source.getConfig(), true);
// Allocate memory for Renderscript to work with
final Allocation input = Allocation.createFromBitmap(rs, source, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
final Allocation output = Allocation.createTyped(rs, input.getType());
// Load up an instance of the specific script that we want to use.
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setInput(input);
// Set the blur radius
script.setRadius(25);
// Start the ScriptIntrinisicBlur
script.forEach(output);
// Copy the output to the blurred source
output.copyTo(blurredBitmap);
// We have created a new copy, recycle the source bitmap.
source.recycle();
return blurredBitmap;
}
@Override
public String key() {
return "blur";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment