How to fit Image into ImageView using Glide

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

Answers


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


You would use .centerCrop() or .fitCenter()

Another way to do it is with a custom Transformation


<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 use Glide v4 GlideApp instance of Glide:

GlideApp.with(view.getContext())
        .load(url)
        .centerCrop()
        .into(view);

If use databinding:

@BindingAdapter("glideCropCenter")
public static void setProgress(ImageView view, string url) {
    GlideApp.with(view.getContext())
            .load(url)
            .centerCrop()
            .into(view);
}

In xml:

       <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:glideCropCenter="@{viewModel.url}" />

Need Your Help

crystal report Header row merge with sub items possible?

c# .net formatting crystal-reports-2008

I have a crystal report in which I group items by salesman as shown below:

highcharts column stack label

javascript jquery json highcharts

I want to show a stacklabel with data coming from a json file, something similar to this.