Last active
December 23, 2020 22:51
-
-
Save adavis/6e06d62d27da54e3f736 to your computer and use it in GitHub Desktop.
This is a Picasso transform that blurs and darkens an image.
This file contains 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
private final class BlurTransform implements Transformation { | |
WeakReference<Context> context; | |
public BlurTransform(Context context) { | |
super(); | |
this.context = new WeakReference<>(context); | |
} | |
@Override | |
public Bitmap transform(Bitmap bitmap) { | |
RenderScript rs = RenderScript.create(context.get()); | |
Bitmap source = bitmap.copy(Bitmap.Config.ARGB_8888, true); | |
if (source == null) { | |
return null; | |
} | |
Allocation input = Allocation.createFromBitmap(rs, source, | |
Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); | |
Allocation output = Allocation.createTyped(rs, input.getType()); | |
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); | |
script.setRadius(17); | |
script.setInput(input); | |
script.forEach(output); | |
output.copyTo(source); | |
// also darken the image | |
Paint paint = new Paint(); | |
ColorFilter filter = new LightingColorFilter(0xFF444444, 0x66666666); | |
paint.setColorFilter(filter); | |
Canvas canvas = new Canvas(source); | |
canvas.drawBitmap(source, 0, 0, paint); | |
bitmap.recycle(); | |
return source; | |
} | |
@Override | |
public String key() { | |
return "blur"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment