如何使用Glide将Image放入ImageView

时间:2015-12-01 14:14:51

标签: android image android-glide

我关心的是如何使用 Glide 将图像使用android:scaleType="fitXY"放入图像。

我的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

enter image description here

7 个答案:

答案 0 :(得分:64)

在Glide v4上,您必须创建一个RequestOptions对象,如下所示:

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

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

More info

答案 1 :(得分:43)

您可以使用$.ajax({ url: "../MyController/myFunction?myStringparameter=" + myString, type: "POST", processData: false, contentType: "application/json; charset=utf-8", data: JSON.stringify({ obj: tabJsonObject}) centerCrop()方法:

fitCenter()

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

您还可以在以下网址找到更多信息:https://futurestud.io/tutorials/glide-image-resizing-scaling

答案 2 :(得分:6)

您可以使用.centerCrop().fitCenter()

另一种方法是使用自定义Transformation

答案 3 :(得分:4)

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

答案 4 :(得分:1)

您可以调用.override(horizo​​ntalSize,verticalSize)。在将其显示在ImageView中之前,将调整图像的大小。

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

答案 5 :(得分:0)

如果使用Glide的Glide v4 GlideApp实例:

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}" />

答案 6 :(得分:0)

我的解决方案是忽略 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)
相关问题