-
-
Save chrisjenx/4a92a602c4dc323e1da1 to your computer and use it in GitHub Desktop.
BlurTransformer for Picasso using renderscript lib.
This file contains hidden or 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
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