Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretch Image to fit Image View using Glide

I want to make an image fit into my image view. I am loading the image programmatically using Glide. It does load the image but does not stretch it to fit my entire image view. It leaves blank spaces on both sides like this

I want to display my entire image without cropping so I can't use

android:scaleType="centerCrop"

There's a way to fit the entire image into my image view by using

android:background="@drawable/image"

It stretches my image to fit the entire image view. But how can I achieve the same if i am loading the image using Glide ?

like image 616
Ashutosh Patoa Avatar asked Apr 24 '18 11:04

Ashutosh Patoa


People also ask

How do you change the size of a picture on Glide?

With Glide, if the image should not be automatically fitted to the ImageView , call override(horizontalSize, verticalSize) . This will resize the image before displaying it in the ImageView .

How do you compress images in Glide?

centerCrop() is a cropping technique that scales the image so that it fills the requested bounds of the ImageView and then crops the extra. The ImageView will be filled completely, but the entire image might not be displayed. RequestOptions myOptions = new RequestOptions() . centerCrop(); Glide.

What is Glide used for?

Glide is an Image Loader Library for Android developed by bumptech and is a library that is recommended by Google. It has been used in many Google open source projects including Google I/O 2014 official application. It provides animated GIF support and handles image loading/caching.


2 Answers

I finally got the answer to it. Simply use

android:scaleType="fitXY"

in the xml ImageView tag like this and load image using Glide or BitmapFactory without using any scaleType attribute there.

<ImageView
 android:id="@+id/image_view"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:scaleType="fitXY"/>

Load Image to the image View

Glide.with(this).load(imageUrl).into((ImageView)findViewById(R.id.image_view));
like image 118
Ashutosh Patoa Avatar answered Sep 20 '22 12:09

Ashutosh Patoa


You can use centerCrop method.

Glide.with(context).load(url).centerCrop().placeholder(R.drawable.default_image).into(img);     
like image 28
Dnyaneshwar Panchal Avatar answered Sep 20 '22 12:09

Dnyaneshwar Panchal