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)

使用效果:

使用后

文中用到的三個圖片


這就是文中所要解決的圖片

第二張圖片

第三張圖片
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、簡介 在泰國舉行的谷歌開發(fā)者論壇上,谷歌為我們介紹了一個名叫Glide的圖片加載庫,作者是bumptech。這...
    天天大保建閱讀 7,753評論 2 28
  • 7.1 壓縮圖片 一、基礎(chǔ)知識 1、圖片的格式 jpg:最常見的圖片格式。色彩還原度比較好,可以支持適當(dāng)壓縮后保持...
    AndroidMaster閱讀 2,692評論 0 13
  • 圖片框架千千萬,使用Glide最方便 (個人感覺,僅供參考) 之前使用過Picasso,一直都覺得很好用,直...
    Discredited閱讀 638評論 2 4
  • Glide的強(qiáng)大和靈活相信不需要多介紹了 本文使用Glide版本為4.8.0,因為使用的Java語言進(jìn)行開發(fā),涉及...
    MrTrying閱讀 23,223評論 2 24
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,621評論 1 32

友情鏈接更多精彩內(nèi)容