I've been using Glide to load images in my app. I've a custom transformation which I'm using while loading an image in ImageView
.
The problem is I want to apply my custom transformation & centerCrop
both on the image fetched. But Glide is using only my custom transformation and displaying the image in ImageView
with fitXY
.
Here is my code:
Glide.with(context)
.load(uri)
.placeholder(R.drawable.image_id)
.transform(new CustomTransformation(context))
.centerCrop()
.into(imageView);
How do I achieve the desired result? Any help would be much appreciated.
Glide is an Image Loader Library for Android developed by bumptech and is a library that is recommended by Google. It has been used in many Google open source projects including Google I/O 2014 official application. It provides animated GIF support and handles image loading/caching.
GitHub - bumptech/glide: An image loading and caching library for Android focused on smooth scrolling. Skip to content Toggle navigation. Product. Actions. Automate any workflow.
In Glide v4.6.1, I found that the MultiTransformation
class makes this easy:
MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(new CustomTransformation(), new CircleCrop());
Glide.with(DemoActivity.this).load(file)
.apply(RequestOptions.bitmapTransform(multiTransformation))
.into(mPreviewImageView);
Make your own CustomTransformation
which extends CenterCrop
, then when overriding transform()
call super
before doing your custom transformation.
For example:
Glide.with(Context)
.load(url)
.asBitmap()
.transform(new CenterCrop(context) {
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
// Call super to have your image center cropped
toTransform = super.transform(pool, toTransform, outWidth, outHeight);
// Apply your own custom transformation
return ImageUtils.fastblur(toTransform, BLUR_RADIUS);
}
@Override
public String getId() {
return "com.example.imageid"
}
})
.placeholder(placeholder)
.into(imageView);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With