Created
December 23, 2018 20:37
-
-
Save SangsooNam/6c01f2932daf98df30f796d0de141444 to your computer and use it in GitHub Desktop.
Picasso BlurTransformation
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 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