Glide4.0同时使用RoundedCorners与CenterCrop

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)

使用效果:

使用后

文中用到的三个图片


这就是文中所要解决的图片

第二张图片

第三张图片
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫Glide的图片加载库,作者是bumptech。这...
    天天大保建阅读 7,600评论 2 28
  • 7.1 压缩图片 一、基础知识 1、图片的格式 jpg:最常见的图片格式。色彩还原度比较好,可以支持适当压缩后保持...
    AndroidMaster阅读 2,584评论 0 13
  • 图片框架千千万,使用Glide最方便 (个人感觉,仅供参考) 之前使用过Picasso,一直都觉得很好用,直...
    Discredited阅读 598评论 2 4
  • Glide的强大和灵活相信不需要多介绍了 本文使用Glide版本为4.8.0,因为使用的Java语言进行开发,涉及...
    MrTrying阅读 22,988评论 2 24
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,156评论 1 32