我有以下Glide代码将来自请求的图像绑定到ImageViews。为了列出图像,我使用RecyclerView及其GridLayoutManager作为其布局管理器:
val imgUri = imgUrl.toUri().buildUpon().scheme("http").build()
Glide.with(imgView.context)
.load(imgUri)
.apply(RequestOptions()
.placeholder(R.drawable.loading)
.error(R.drawable.ic_broken_image))
.into(imgView)
这是我的回收站视图每个项目的布局:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="item"
type="..."/>
<variable
name="itemCallback"
type="..." />
</data>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/white"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="?attr/colorPrimaryDark"
android:scaleType="fitCenter"
android:layout_margin="5dp"
android:adjustViewBounds = "true"
app:itemImageUrl="@{item.itemImage}"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp"
android:onClick="@{() -> itemCallback.onClick(item)}">
<TextView
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item Name"
android:textAppearance="?attr/textAppearanceHeadline6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:itemName="@{item.itemName}"/>
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</layout>
这是我的包含recyclerview的布局:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="itemListViewModel"
type="..." />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.ItemListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/item_list_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:padding="6dp"
android:clipToPadding="false"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:spanCount="3"
app:itemListData="@{itemListViewModel.itemList}"
tools:listitem="@layout/item_list_item"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我想要的是每个项目的图像都应填充整个图像视图,同时仍保持相同的宽高比。但是我得到的是:
如您所见,图像位于中间,没有占据整个ImageView
空间。我在这里想念什么? centerCrop
属性将填满整个屏幕,但不会显示整个图像。
[在我使用Glide之前,我使用了毕加索,并且它具有称为fit()
的出色方法,该方法完全可以完成我上面描述的工作-在保持宽高比的同时填充整个ImageView
。 Glide中也有类似的方法吗?
尝试一下:
Glide.with(imgView.context)
.load(imgUri)
.apply(RequestOptions()
.placeholder(R.drawable.loading)
.error(R.drawable.ic_broken_image)
.fitCenter())
.into(imgView)
或者,您也可以在ImageView
中添加此属性:
android:scaleType="fitCenter"