7

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.

Anjani
  • 1,533
  • 3
  • 15
  • 26

6 Answers6

10

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);
Nic Dahlquist
  • 1,005
  • 12
  • 15
4

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);
Orr Matarasso
  • 771
  • 1
  • 7
  • 17
2

You can apply multiple transformations like this:

 Glide.with(getContext())
            .load(url)
            .bitmapTransform(new CenterCrop(getContext()), new BlurTransformation(getContext(), BLUR_RADIUS), new GrayscaleTransformation(getContext()))
            .into(imageView);
2

In Glide 4.11 we can use MultiTransformation with a list of transformations:

GlideApp.with(imageView)
    .load(url)
    .transform(MultiTransformation(CenterCrop(), RoundedCorners(10), Rotate(30)))
    // .error(...)
    .into(imageView)
CoolMind
  • 26,736
  • 15
  • 188
  • 224
2

2021 syntax ...

It seems you now need "new" all over:

protected void setPicture() {

    String url = doc.get("image");

    // the rounding of the products_box is 12dp
    int twelve = _dp(12);

    Glide.with(itemView.getContext())
            .load(url)
            .transform(
               new MultiTransformation(
                  new CenterCrop(),
                  new RoundedCorners(twelve)))
            .into(happy_product_image);
}

(Note that, as usual, you'll need to convert the DP amount, example https://stackoverflow.com/a/66459077/294884 )

Fattie
  • 27,874
  • 70
  • 431
  • 719
1

Better way (especially with Glide 4 which no longer accepts more than one single BitmapTransformation) is to create a CombinedTransformation, like this:

class CombinedTransformation(vararg val transformations: Transformation<Bitmap>) 
        : Transformation<Bitmap> {
    override fun transform(context: Context, resource: Resource<Bitmap>, 
                           outWidth: Int, outHeight: Int): Resource<Bitmap> {
        var out = resource
        for (transformation in transformations) {
            out = transformation.transform(context, out, outWidth, outHeight)
        }
        return out
    }
    override fun updateDiskCacheKey(messageDigest: MessageDigest) {
        transformations.forEach { it.updateDiskCacheKey(messageDigest) }
    }
}

Then use it like so (again, Glide 4):

val options = RequestOptions()
options.transform(CombinedTransformation(CenterCrop(...), BlurTransformation(...)))
Glide.with(context).load(url).apply(options).into(imageView)
Thomas Keller
  • 5,933
  • 6
  • 48
  • 80
  • Thanks for `CombinedTransformation(CenterCrop(...), BlurTransformation(...))`! I understood that we can use `MultiTransformation(CenterCrop(), RoundedCorners(10))`. – CoolMind Sep 08 '20 at 16:41