我尝试在Glide库(4.9.0)的帮助下将一堆图像加载到RecyclerView中。我的代码是这样的:ViewHolder
inner class HorizontalImageViewHolder(v: View) : RecyclerView.ViewHolder(v) {
fun bind(item: SliderImage) {
val url = (item.icon as UrlIcon).url
Glide.with(itemView)
.load(url)
.transform(RoundedCorners(40))
.into(itemView.ivImage)
}
}
布局
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_margin="8dp"
android:background="@color/orangey_yellow"
android:layout_width="@dimen/slider_image_width"
android:layout_height="@dimen/slider_image_height">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/backgrounds/scenic[17]" />
</androidx.constraintlayout.widget.ConstraintLayout>
setData
fun setData(items: List<SliderImage>) {
data.clear()
data.addAll(items)
notifyDataSetChanged()
}
但是当我滚动项目时,我看到它们具有这样的不同的圆角大小
有人知道这是什么问题吗?预先感谢!
顺便说一下。
问题出在不同的图像分辨率上。因此,在使用滑角对图像角进行四舍五入并期望四舍五入在视觉上是相等的之前,也请覆盖分辨率也要相等。
我这样做]
val transformations = mutableListOf<Transformation<Bitmap>>(CenterCrop())
if (cornerRadius > 0) transformations.add(RoundedCorners(cornerRadius))
//other transformations here
Glide.with(iv)
.load(url)
.apply {
if (width > 0 && height > 0) {
this.override(width, height)//this one u need before applying transforamtions to make sure roundings will look equal
}
this.transform(*transformations.toTypedArray())
}
.into(iv)