Glide - RecyclerView 中不同的圆角
Glide - Different rounded corners in RecyclerView
我尝试借助 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>
设置数据
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)
我尝试借助 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>
设置数据
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)