Glide: Is there a way to set max size for a drawable in glide?

Created on 14 Sep 2017  路  6Comments  路  Source: bumptech/glide

I have a problem ,currently. I want to set a max size and only when the really drawable size is large than the max size then scale down with the ratio maintained.

Is there a way achieve it?

        GlideApp.with(mTextView)
                .load(source)
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        // the max width of the resource is the width of the mTextView. 
                        // because I want to show the drawable in the textview.
                    }
                });

Glide Version (4.1.1)

question stale

All 6 comments

You can either write a custom DownsampleStrategy or a custom BitmapTransformation or both. In both cases you get the input width and height and can decide how to scale however you wish in response.

If you do implement a custom BitmapTransformation be sure to implement equals/hashcode so that Glide's cache keys function properly.

Existing downsample strategies that might be helpful for reference can be found here: https://github.com/bumptech/glide/blob/b2fb2297dcd64398a9415677b6d566648d52a7a1/library/src/main/java/com/bumptech/glide/load/resource/bitmap/DownsampleStrategy.java.

The FitCenter transformation logic that scales an image maintaining its aspect ratio can be found here: https://github.com/bumptech/glide/blob/b2fb2297dcd64398a9415677b6d566648d52a7a1/library/src/main/java/com/bumptech/glide/load/resource/bitmap/TransformationUtils.java#L118

@sjudd Thanks for your reply, I found another simple way to achieve it. Code like below:

       int maxSize = imgview.getWidth();        
      GlideApp.with(getContext())
                .asBitmap()
                .load(mImageUrl)
                .fitCenter()
                .into(new SimpleTarget<Bitmap>(maxSize, SIZE_ORIGINAL) {
                    @Override
                    public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                       // do something ... 
                });

In my test , I found it can meet my needs.

Keep in mind that fitCenter will upscale, so if you have an image smaller than your given size, it may be scaled up until it matches. If you only want to downscale, you use centerInside() instead.

@sjudd I know the fitCenter scaletype of the imageview maybe upscale . However the fitCenter of a glide target doesn't upscale actually in my test.
------new update-------
I found that if one of the width and height set to SIZE_ORIGINAL then glide will not upscale.
Otherwise, It will upscale. But the centerInside will not upcsale all the time.
Thanks for your remind!

I guess BitmapTransformation may help.I use TransformationUtils.centerCrop
```java
public class BigBitmapTransformation extends BitmapTransformation {
private static final String ID = "BigBitmapTransformation";
private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
private static final Paint DEFAULT_PAINT = new Paint(TransformationUtils.PAINT_FLAGS);

private int maxWidth;

public BigBitmapTransformation(int maxWidth) {
    this.maxWidth = maxWidth;
}

@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
    if (toTransform.getHeight() <= maxWidth) {
        return toTransform;
    }
    return TransformationUtils.centerCrop(pool, toTransform,
            toTransform.getWidth() * maxWidth / toTransform.getHeight(), maxWidth);
}

@Override
public boolean equals(Object o) {
    return o instanceof BigBitmapTransformation;
}

@Override
public int hashCode() {
    return ID.hashCode();
}

@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
    messageDigest.update(ID_BYTES);
}

}

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings