54

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

enter image description here

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
N J
  • 27,217
  • 13
  • 76
  • 96

7 Answers7

89

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
  • 5
    how to set scaletype for placeholder ? – Anand Savjani Sep 19 '16 at 06:18
  • 3
    On Android use this: Glide.with(getActivity()).load(imageSourceUrl) .apply(new RequestOptions().centerCrop()) – Alexei Sep 27 '18 at 10:44
  • 6
    Okay, the above answer shows to fit the image with **fitCenter** or **centerCrop**. But what about **fitXY**? Is there anyway to set fitXY instead of any other parameters? – dipakbari4 May 14 '19 at 20:00
79

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

OriolJ
  • 2,762
  • 1
  • 28
  • 22
10
<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);
Ahamadullah Saikat
  • 4,437
  • 42
  • 39
6

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

Another way to do it is with a custom Transformation

Cristan
  • 12,083
  • 7
  • 65
  • 69
Empty2k12
  • 475
  • 12
  • 34
  • Hi @Empty2k12, can you elaborate on custom Transformation? I have a custom masking transformation but I am unable to center fit or correctly crop the image keeping the aspect ratio consistent. – Bijon Desai Oct 12 '20 at 19:51
3

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}" />
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
1

You can call .override(horizontalSize, verticalSize). This will resize the image before displaying it in the ImageView.

Glide.with(context)
     .load(url)
     .override(horizontalSize, verticalSize)//resizes the image to these dimensions (in pixel).
     .into(img);
1

My solution is to ignore BitmapTransformation for FIT_XY scale type.
Also set scale type to the corresponding types like below,

        val options = RequestOptions()

        mDesignOptions?.bgScaleType?.let {
            mScaleType = it
        }

        if (mScaleType == Settings.SCALE_TYPE_CENTER_CROP) {
            imgView.scaleType = ImageView.ScaleType.CENTER_CROP
            options.transform(CenterCrop())
        } else if (mBgScaleType == Settings.SCALE_TYPE_ORIGINAL) {
            imgView.scaleType = ImageView.ScaleType.FIT_CENTER
            options.transform(FitCenter())
        } else {
            imgView.scaleType = ImageView.ScaleType.FIT_XY
        }


        Glide.with(applicationContext)
            .load(imgPath) // Uri of the Photo
            .apply(options)
            .into(imgView)
Jayakrishnan
  • 4,457
  • 29
  • 29