Updated 28 February 2020
Glide BitmapTransformation can be used as an image manipulation before the image is being displayed.
For example, Crop image, blur image, Mask, Color, etc.
In this blog, we will blur the image with the help of class BitmapTransformation provided by glide.
Step1: Added glide dependencies.
1 2 3 |
// For Image loading api "com.github.bumptech.glide:glide:4.10.0" annotationProcessor "com.github.bumptech.glide:compiler:4.10.0" |
Step2: Extend BitmapTransformation class and Override method transform
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; import java.security.MessageDigest; public class GlideBlurTransformation extends BitmapTransformation { private RenderScript rs; public GlideBlurTransformation(Context context) { super(); rs = RenderScript.create(context); } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { Bitmap blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true); Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setInput(input); // Set the blur radius script.setRadius(20); script.forEach(output); // Copy the output to the blurred bitmap output.copyTo(blurredBitmap); return blurredBitmap; } @Override public void updateDiskCacheKey(MessageDigest messageDigest) { messageDigest.update("blur transformation".getBytes()); } } |
Step3: Transform image
For transforming image, you need to pass an instance of your transformation class as a parameter to .transform()
.
1 |
Glide.with(context).load(imageUrl).transform(BlurTransformation(context )).into(imageView) |
You can write your logic in the method .transform()
to apply other transformations on the bitmap image.
References:
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.