Glide同時(shí)使用RoundedCorner和CenterCrop,在圖片寬高與ImageView不一致對(duì)情況下尘惧,圓角無(wú)法正常顯示棚饵。
如圖:
item中ImageView代碼:
<ImageView
android:id="@+id/iv_photo"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"/>
Glide代碼:
Glide.with(context).load(url)
.apply(RequestOptions.bitmapTransform(RoundedCorners(5)))
.into(imageView)
解決辦法就是在CenterCop之后再RoundedCorner
繼承BitmapTransformation属韧,重寫transform即可:
package cn.jingnan.weidget
import android.graphics.Bitmap
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.load.resource.bitmap.TransformationUtils
import java.security.MessageDigest
/**
* Author:jingnan
* Time:2019-08-28/15
* Content:Glide同時(shí)使用RoundedCorner和CenterCrop,在圖片寬高與ImageView不一致對(duì)情況下
*/
class RoundedCornerCenterCrop(val radius: Int = 0) : BitmapTransformation() {
override fun updateDiskCacheKey(messageDigest: MessageDigest) {
}
override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap {
val bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight)
return TransformationUtils.roundedCorners(pool, bitmap, radius)
}
}
使用代碼:
Glide.with(context).load(url)
.apply(RequestOptions.bitmapTransform(RoundedCornerCenterCrop(5)))
.into(imageView)
使用效果:
文中用到的三個(gè)圖片