Glide: Scaling Image in ImageView

Created on 1 Apr 2017  路  5Comments  路  Source: bumptech/glide

I have an ImageView with match_parent width and wrap_content height like this:

<ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

How to load an image (http://lorempixel.com/400/600/) using Glide that fit with ImageView and keeps the aspect ratio of the image?

question

Most helpful comment

wrap_content defaults to screen height so if your ratio would overshoot that you get a fitted image. What you probably want is .override(screenWidth, Target.SIZE_ORIGINAL).fitCenter(), to display images taller than the screen, but consider that it's probably bad UX (IMO).

All 5 comments

Have you tried:

ImageView imageView = (ImageView)findViewById(R.id.image);
Glide.with(this).load("http://lorempixel.com/400/600/").into(imageView);

The default ImageView.scaleType is FIT_CENTER, which Glide recognizes and uses .fitCenter() implicitly. See source code of GenericRequestBuilder.into(ImageView) method.

I've tried your solution but the image doesn't fit with the width of the layout. The layout_width of my ImageView is match_parent, so the image's width should follow the layout width which is match_parent. I've tried using fitXY, but it doesn't keep the aspect ratio of the image. Look at the result below:

Load image from http://lorempixel.com/400/400/ (this is what I want in 400x600 and other ratios, the width is match_parent):
image

Load from http://lorempixel.com/400/600/ using fitXY:
image

Load from http://lorempixel.com/400/600/ using default configuration:
image

and why GenericRequestBuilder.into(ImageView) doesn't handle all scaleType?
thank you :)

wrap_content defaults to screen height so if your ratio would overshoot that you get a fitted image. What you probably want is .override(screenWidth, Target.SIZE_ORIGINAL).fitCenter(), to display images taller than the screen, but consider that it's probably bad UX (IMO).

It does support all the scale types, the default applies to the non-mentioned ones.

that's true to use Target.SIZE_ORIGINAL

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sergeyfitis picture sergeyfitis  路  3Comments

sant527 picture sant527  路  3Comments

kenneth2008 picture kenneth2008  路  3Comments

MrFuFuFu picture MrFuFuFu  路  3Comments

ersen-osman picture ersen-osman  路  3Comments