My concern is how to fit image using android:scaleType="fitXY"
into image using Glide.
My ImageView
is
<ImageView
android:id="@+id/img_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitXY" />
Loads image using Glide like this
Glide.with(context).load(url).placeholder(R.drawable.default_image).into(img);
but image is not getting fit into ImageView
it show space on image either side as shown in screen I need it fitXY
try adding android:scaleType="fitXY" to your ImageView . This will modify the aspect ratio if the original image is not squared. fitXY will almost always change the aspect ratio of the image.
However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="..." . src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView.
Add an explicit width or height to the ImageView by setting layout_width=500dp in the layout file. Call . override(width, height) during the Glide load and explicitly set a width or height for the image such as: GlideApp.
You can use centerCrop()
or fitCenter()
methods:
Glide.with(context)
.load(url)
.centerCrop()
.placeholder(R.drawable.default_image)
.into(img)
or
Glide.with(context)
.load(url)
.fitCenter()
.placeholder(R.drawable.default_image)
.into(img)
You can also find more information at: https://futurestud.io/tutorials/glide-image-resizing-scaling
On Glide v4 you have to create a RequestOptions object, like this:
RequestOptions options = new RequestOptions();
options.centerCrop();
Glide.with(fragment)
.load(url)
.apply(options)
.into(imageView);
More info
<android.support.v7.widget.AppCompatImageView
android:id="@+id/ivPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
.....................................
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
AppCompatImageView ivPhoto = findViewById(R.id.ivPhoto);
Glide.with(this)
.load("https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg")
.apply(new RequestOptions()
.placeholder(R.drawable.place_holder_gallery)
.error(R.drawable.ic_no_image)
)
.into(ivPhoto);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With