Glide根據(jù)控件長(zhǎng)寬,以一邊為基準(zhǔn)等比例縮放圖片,最后居中

網(wǎng)上關(guān)于BitmapTransformation也沒(méi)啥好的例子,我下了份源碼,抄抄改改,不知道改的對(duì)不對(duì),反正效果實(shí)現(xiàn)了,一開(kāi)始我直接返回new Bitmap,看源碼都不這么用,源碼都是從BitmapPool去拿的Bitmap了,不知道是不是要復(fù)用,反正抄就完事了。最后效果實(shí)現(xiàn)了

import android.graphics.Bitmap
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import java.security.MessageDigest

class GlideBitmapTransformation : BitmapTransformation() {


    private val ID = "com.bumptech.glide.transformations.GlideBitmapTransformation"
    private val ID_BYTES: ByteArray = ID.toByteArray(charset(STRING_CHARSET_NAME))

    override fun updateDiskCacheKey(messageDigest: MessageDigest) {
        messageDigest.update(ID_BYTES)
    }

    override fun equals(o: Any?): Boolean {
        return o is GlideBitmapTransformation
    }

    override fun hashCode(): Int {
        return ID.hashCode()
    }

    override fun transform(
        pool: BitmapPool,
        toTransform: Bitmap,
        outWidth: Int,
        outHeight: Int
    ): Bitmap {

        return TransformationUtil.centerScale(pool, toTransform, outWidth, outHeight)

    }


}



import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.TransformationUtils
import java.util.concurrent.locks.ReentrantLock

object TransformationUtil {

    private val BITMAP_DRAWABLE_LOCK = ReentrantLock()

    private val DEFAULT_PAINT = Paint(TransformationUtils.PAINT_FLAGS)


    fun centerScale(
        pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int
    ): Bitmap {
        if (toTransform.getWidth() == outWidth && toTransform.getHeight() == outHeight) {

            return toTransform
        }
        val config = getNonNullConfig(toTransform)

        val toReuse = pool[outWidth, outHeight, config]

        val width: Int = toTransform.getWidth()
        val height: Int = toTransform.getHeight()
        val scaleWidth: Float = outWidth.toFloat() / width
        val scaleHeight: Float = outHeight.toFloat() / height
        val matrix = Matrix()
        val dx: Float
        val dy: Float

        if (scaleWidth > scaleHeight) {


            dx = (outWidth - toTransform.getWidth() * scaleHeight) * 0.5f
            dy = 0f

            matrix.postScale(scaleHeight, scaleHeight)

        } else {

            dx = 0f
            dy = (outHeight - toTransform.getHeight() * scaleWidth) * 0.5f

            matrix.postScale(scaleWidth, scaleWidth)
        }

        matrix.postTranslate(dx, dy)

        applyMatrix(toTransform, toReuse, matrix)

        return toReuse
    }

    private fun getNonNullConfig(bitmap: Bitmap): Bitmap.Config? {
        return if (bitmap.config != null) bitmap.config else Bitmap.Config.ARGB_8888
    }

    private fun applyMatrix(
        inBitmap: Bitmap, targetBitmap: Bitmap, matrix: Matrix
    ) {
        BITMAP_DRAWABLE_LOCK.lock()
        try {
            val canvas = Canvas(targetBitmap)
            canvas.drawBitmap(inBitmap, matrix, DEFAULT_PAINT)
            clear(canvas)
        } finally {
            BITMAP_DRAWABLE_LOCK.unlock()
        }
    }

    private fun clear(canvas: Canvas) {
        canvas.setBitmap(null)
    }
}





        Glide.with(holder.itemView)
            .load(data.filePath)
            .transform(GlideBitmapTransformation())
            .into(holder.imageView)

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

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