Glide4.0同时使用RoundedCorners与Cente

作者: 筱南独舞 | 来源:发表于2019-08-28 16:17 被阅读0次

Glide同时使用RoundedCorner和CenterCrop,在图片宽高与ImageView不一致对情况下,圆角无法正常显示。

如图:

使用前

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同时使用RoundedCorner和CenterCrop,在图片宽高与ImageView不一致对情况下
 */
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)

使用效果:

使用后

文中用到的三个图片


这就是文中所要解决的图片
第二张图片
第三张图片

相关文章

网友评论

    本文标题:Glide4.0同时使用RoundedCorners与Cente

    本文链接:https://www.haomeiwen.com/subject/yycmectx.html