Android矩阵Matrix实现Glide图像fitCenter转换为centerCrop,Kotlin

时间:2024-12-06 22:22:07

Android矩阵Matrix实现Glide图像fitCenter转换为centerCrop,Kotlin

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image1"
        android:layout_width="500px"
        android:layout_height="500px"
        android:background="@android:color/holo_green_light" />

    <ImageView
        android:id="@+id/image2"
        android:layout_width="500px"
        android:layout_height="500px"
        android:background="@android:color/holo_blue_light" />

    <ImageView
        android:id="@+id/image3"
        android:layout_width="500px"
        android:layout_height="500px"
        android:background="@android:color/holo_red_light" />

    <ImageView
        android:id="@+id/image4"
        android:layout_width="500px"
        android:layout_height="500px"
        android:background="@android:color/holo_orange_light" />

</LinearLayout>

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.RectF
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target

class MainActivity : AppCompatActivity() {
    companion object {
        const val TAG = "fly"
        const val SIZE = 500
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val image1 = findViewById<ImageView>(R.id.image1)
        val image2 = findViewById<ImageView>(R.id.image2)
        val image3 = findViewById<ImageView>(R.id.image3)
        val image4 = findViewById<ImageView>(R.id.image4)

        Glide.with(this)
            .load(R.mipmap.pic)
            .fitCenter()
            .override(SIZE)
            .into(image1)

        Glide.with(this)
            .load(R.mipmap.pic)
            .centerCrop()
            .override(SIZE)
            .into(image2)

        Glide.with(this)
            .asBitmap()
            .load(R.mipmap.pic)
            .fitCenter()
            .addListener(object : RequestListener<Bitmap> {
                override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>, isFirstResource: Boolean): Boolean {
                    return false
                }

                override fun onResourceReady(
                    resource: Bitmap,
                    model: Any,
                    target: Target<Bitmap>?,
                    dataSource: DataSource,
                    isFirstResource: Boolean
                ): Boolean {
                    val bmp = transformToCenterCrop(resource)
                    image4.setImageBitmap(bmp)
                    return false
                }
            })
            .override(SIZE)
            .into(image3)
    }

    fun transformToCenterCrop(bitmap: Bitmap): Bitmap {
        val w = bitmap.width
        val h = bitmap.height
        val bmp = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888)
        val c = Canvas(bmp)
        c.drawColor(Color.LTGRAY)

        val mini = Math.min(w, h)
        val left = (w - mini) / 2f
        val top = (h - mini) / 2f
        val right = (w + mini) / 2f
        val bottom = (h + mini) / 2f

        val srcRectF = RectF(left, top, right, bottom)
        val dstRectF = RectF(0f, 0f, SIZE.toFloat(), SIZE.toFloat())
        val mx = Matrix()
        mx.setRectToRect(srcRectF, dstRectF, Matrix.ScaleToFit.CENTER)
        c.drawBitmap(bitmap, mx, null)

        return bmp
    }
}

转换为centerCrop耗时约1ms。

Android矩阵Matrix裁切setRectToRect拉伸Bitmap替代Bitmap.createScaledBitmap缩放,Kotlin-****博客文章浏览阅读1.2k次,点赞7次,收藏14次。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。基础上,把剪切的区域从矩形Rect变为圆形的Path,当手指在上面的ImageView移动时候,下面同等大小对应的坐标区域显示“剪切”出来的圆形图。_createscaledbitmaphttps://blog.****.net/zhangphil/article/details/135961734