glide icon indicating copy to clipboard operation
glide copied to clipboard

CenterCrop upscales by default

Open weidongjian opened this issue 8 years ago • 10 comments

Glide Version:Glide 4.6.1

Integration libraries:Glide 4.6.1

Issue details / Repro steps / Use case background:

Glide load line / GlideModule (if any) / list Adapter code (if any):

private String smalImagelUrl= "http://olal87ycc.bkt.clouddn.com/5a4b556e8b171m8ifn4gse4280.jpg!thumb120";
GlideApp.with(this).load(smalImagelUrl).into(imageView);

Layout XML:

<FrameLayout xmlns:android="...>
<ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
<>

image original size is 120 * 213, and imageview size is 1080 * 1920, and the result bitmap size is 1080 * 1920. the real image size is larger than original size. i think if original size is small than view size, result bitmap size should used original size.

I know can use override to control bitmap size, but in my cast, the image original size is unknown in advance,

当图片的尺寸小于视图的尺寸的时候,应该使用图片的尺寸,两者取小的那个。

weidongjian avatar Mar 19 '18 13:03 weidongjian

you can configure it with DownsampleStrategy

RequestOptions options = new RequestOptions()
options.downsample(DownsampleStrategy.CENTER_INSIDE)

https://github.com/bumptech/glide/blob/master/library/src/main/java/com/bumptech/glide/load/resource/bitmap/DownsampleStrategy.java

kukuhyoniatmoko avatar Mar 21 '18 02:03 kukuhyoniatmoko

@kukuhyoniatmoko I tried, but not work, the bitmap size is also same as image size, when image is match-parent

weidongjian avatar Mar 21 '18 12:03 weidongjian

@kukuhyoniatmoko is on the right track, but use the corresponding transformation method instead: http://bumptech.github.io/glide/javadocs/470/com/bumptech/glide/request/RequestOptions.html#centerInside--

sjudd avatar Mar 22 '18 15:03 sjudd

GlideApp.with(this).load(smallUrl).centerInside().into(imageView); centerinside did work at this case, but is not my desire answer

But if I want image scaleType set to CenterCrop, how should I do?

GlideApp.with(this).load(smallUrl).centerInside().centerCrop().into(imageView); this not work, and bitmap is also as large as imageview

weidongjian avatar Mar 23 '18 01:03 weidongjian

Is this the desired default behaviour? Why would you use more memory than necessary? Glide should never be upsampling images..

gajicm93 avatar Mar 23 '18 08:03 gajicm93

I find a solution, if it work? In BitmapTransformation class , change method transform( @NonNull Context context, @NonNull Resource<Bitmap> resource, int outWidth, int outHeight) as below:

public final Resource<Bitmap> transform(
            @NonNull Context context, @NonNull Resource<Bitmap> resource, int outWidth, int outHeight) {
        if (!Util.isValidDimensions(outWidth, outHeight)) {
            throw new IllegalArgumentException(
                    "Cannot apply transformation on width: " + outWidth + " or height: " + outHeight
                            + " less than or equal to zero and not Target.SIZE_ORIGINAL");
        }
        BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
        Bitmap toTransform = resource.get();
        int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth;
        int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight;

        if (targetWidth > toTransform.getWidth() || targetHeight > toTransform.getHeight()) {
            float widthRatio = targetWidth * 1.0f / toTransform.getWidth();
            float heightRatio = targetHeight * 1.0f / toTransform.getHeight();
            float finalRatio = Math.max(widthRatio, heightRatio);
            targetWidth = (int) (targetWidth / finalRatio);
            targetHeight = (int) (targetHeight / finalRatio);
        }

        Bitmap transformed = transform(bitmapPool, toTransform, targetWidth, targetHeight);

        final Resource<Bitmap> result;
        if (toTransform.equals(transformed)) {
            result = resource;
        } else {
            result = BitmapResource.obtain(transformed, bitmapPool);
        }
        return result;
    }

make target size always small than original size

weidongjian avatar Mar 26 '18 01:03 weidongjian

Right, you'd need a custom transformation to avoid upscaling for CenterCrop. The default behavior of that Transformation upscales. You can apply multiple transformations with: http://bumptech.github.io/glide/javadocs/460/com/bumptech/glide/request/RequestOptions.html#transforms-com.bumptech.glide.load.Transformation...-. Applying multiple transformations here won't help you though because CenterCrop will still upscale.

sjudd avatar Mar 26 '18 20:03 sjudd

Why the glide default behavior not avaoid upscale bitmap, I think upscale bitmap have no binefit but increase the memory useage I think upscale bitmap is unnessary.

weidongjian avatar Mar 27 '18 01:03 weidongjian

upscaling is the default behavior primarily because it helps with Bitmap re-use which is important on older versions of Android (it produces uniform image sizes) and we don't typically expect people to be displaying images that are smaller than the views they're shown in by any substantial margin.

We've created non-upscaling versions of most other transformations which we could do here. For backwards compatibility reasons we probably don't want to just change the behavior of CenterCrop, though I suppose we could consider it since I don't think it's a behavior people are likely to be relying on.

sjudd avatar Mar 27 '18 15:03 sjudd

disable upscaling behavior and I think should also avoid margin when display smaller image in large views. I would prefer avoid upscale by default, if is not suitable, if it possible set it in GlideBuilder, and I can change this behavior in my project.

weidongjian avatar Mar 28 '18 09:03 weidongjian