Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide Multiple transformations in Android

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.

like image 520
Anjani Avatar asked Aug 01 '15 08:08

Anjani


People also ask

Does glide work on Android?

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.

What is Bumptech Glide?

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.


2 Answers

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);
like image 86
Nic Dahlquist Avatar answered Sep 28 '22 02:09

Nic Dahlquist


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);
like image 30
Orr Matarasso Avatar answered Sep 28 '22 03:09

Orr Matarasso