I am trying to load image from a URL in Image view but because Glide maintains the aspect ratio by default I can't seem to display my entire Image in the view.
Following is my Image View
<ImageView
android:layout_width="match_parent"
android:layout_height="262dp"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_centerHorizontal="true"/>
I have tried following methods.
Glide.with(context)
.load(URL)
.centerCrop()
.crossFade()
.into(ImageView);
It fits the image and crops what is cannot be displayed.
Glide.with(context)
.load(URL)
.fitCenter()
.crossFade()
.into(ImageView);
Loads entire image but does not fill the Image View.
Glide.with(context)
.load(URL)
.override(//Width of Image View from dp to px conversion, //Height of Image View from dp to px conversion)
.centerCrop()
.crossFade()
.into(ImageView);
the last method does seem to work but even in this case Glide tries to maintain aspect ratio and is cropping my image. Is there a workaround to this problem?
Please read the docs on ImageView.scaleType and see some exampes as they may not be immediately clear from the technical definitions.
Glide uses the above to transform images. It's unusual to want to display an image that fills the view and distorts the image, that's why the aspect is kept: think faces or textual logos. Cropping is much more usual, but still doesn't distort the ratio, it achieves kind of a zoom effect.
I think from the available scale types you're looking for android:scaleType="fitXY". Put that in XML and remove all the override/centerCrop/fitCenter calls from Java code.
_As advanced usage you may be able to combine explicit Java transformations with different android:scaleType values to achieve more complex results._
Looks like I was looking in the wrong direction, thanks for pointing to the right one.
Most helpful comment
Please read the docs on
ImageView.scaleTypeand see some exampes as they may not be immediately clear from the technical definitions.Glide uses the above to transform images. It's unusual to want to display an image that fills the view and distorts the image, that's why the aspect is kept: think faces or textual logos. Cropping is much more usual, but still doesn't distort the ratio, it achieves kind of a zoom effect.
I think from the available scale types you're looking for
android:scaleType="fitXY". Put that in XML and remove all theoverride/centerCrop/fitCentercalls from Java code._As advanced usage you may be able to combine explicit Java transformations with different
android:scaleTypevalues to achieve more complex results._