如何使用Glide将图片适配到ImageView中

54

我的问题是如何将使用android:scaleType="fitXY"的图片与使用Glide加载的图片适配。

我的ImageView是:

<ImageView
    android:id="@+id/img_pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:scaleType="fitXY" />

像这样使用 Glide 加载图片

Glide.with(context).load(url).placeholder(R.drawable.default_image).into(img);

但是图片无法适应ImageView,它在图像两侧显示空白,如屏幕所示,我需要fitXY

在此输入图片描述

7个回答

89

您可以使用centerCrop()fitCenter()方法:

Glide.with(context)
     .load(url)
     .centerCrop()
     .placeholder(R.drawable.default_image)
     .into(img)

或者
Glide.with(context)
     .load(url)
     .fitCenter()
     .placeholder(R.drawable.default_image)
     .into(img)

你还可以在此处找到更多信息:https://futurestud.io/tutorials/glide-image-resizing-scaling


5
如何为占位符设置ScaleType? - Anand Savjani
3
在Android中使用Glide库,可以这样写:Glide.with(getActivity()).load(imageSourceUrl) .apply(new RequestOptions().centerCrop())。该代码会加载图片并居中裁剪。 - Alexei
6
好的,以上回答展示了如何使用 fitCentercenterCrop 来适应图像。但是如果要使用 fitXY,是否有任何设置可以实现呢? - dipakbari4

79

在 Glide v4 中,您需要创建一个 RequestOptions 对象,例如:

RequestOptions options = new RequestOptions();
options.centerCrop();

Glide.with(fragment)
    .load(url)
    .apply(options)
    .into(imageView);

更多信息


2
选项.centerCrop()的结果被忽略了。 - quangkid
6
另外还有一个更加简洁的选择:Glide.with(fragment).load(url).apply(RequestOptions.centerCropTransform()).into(imageView); - dfinn

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);

1
那对我的情况完美地起作用了,谢谢。 - João Gabriel

6
您可以使用.centerCrop().centerInside().fitCenter()来实现。另一种方法是使用自定义的Transformation

嗨@Empty2k12,你能详细说明一下自定义转换吗?我有一个自定义遮罩转换,但是我无法使图像居中适合或正确裁剪图像,保持纵横比一致。 - Bijon Desai

3
如果使用Glide v4,需要使用GlideApp实例而不是Glide:
GlideApp.with(view.getContext())
        .load(url)
        .centerCrop()
        .into(view);

如果使用数据绑定:
@BindingAdapter("glideCropCenter")
public static void setProgress(ImageView view, string url) {
    GlideApp.with(view.getContext())
            .load(url)
            .centerCrop()
            .into(view);
}

在XML中:

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

1
我的解决方案是对于FIT_XY的缩放类型忽略BitmapTransformation。 同时将缩放类型设置为以下相应的类型:
        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)

1
您可以调用.override(horizontalSize, verticalSize)方法。这将在 ImageView 中显示图像之前将其调整大小。
Glide.with(context)
     .load(url)
     .override(horizontalSize, verticalSize)//resizes the image to these dimensions (in pixel).
     .into(img);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接