Android矩阵setRectToRect裁剪Bitmap原图Matrix放大,mapRect标记中心区域,Kotlin

Android矩阵setRectToRect裁剪Bitmap原图Matrix放大,mapRect标记中心区域,Kotlin

Kotlin 复制代码
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.util.AttributeSet
import android.util.SizeF
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView


class MainActivity : AppCompatActivity() {
    private var iv: MyImageView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        iv = findViewById(R.id.iv)

        val result = findViewById<ImageView>(R.id.result)
        iv?.setTestImageView(result)
    }
}

class MyImageView : AppCompatImageView {
    private var mSrcBmp: Bitmap? = null
    private var testIV: ImageView? = null

    //这个SizeF是针对原图的没有缩放的宽高。
    //目的是在没有缩放的原图上规划出相当于原图的120f,80f的区域。
    private val mSizeF = SizeF(120f, 80f)

    private var mPathPaint: Paint? = null

    constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {
        //mSrcBmp是原始图大小,没有缩放和拉伸的。
        mSrcBmp = (drawable as BitmapDrawable).bitmap

        mPathPaint = Paint()
        mPathPaint?.style = Paint.Style.STROKE
        mPathPaint?.strokeWidth = 3f
        mPathPaint?.isAntiAlias = true
        mPathPaint?.color = Color.RED
    }

    fun setTestImageView(imageView: ImageView) {
        testIV = imageView
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        //因为所以的原始图片几乎都会被Android缩放后最终展示在手机屏幕上,这里需要计算出原始图片被Android系统缩放了多少比例。
        //本例是一个宽400高300左右的小图,必定会被Android系统拉伸放到ImageView里面。
        val originBmpScaleFactor: Float = (this.width.toFloat()) / (mSrcBmp!!.width.toFloat())

        //这个RectF是相对于原始图框出来的RectF。
        val lineRectF = RectF(
            0f,
            0f,
            mSizeF.width,
            mSizeF.height
        )

        //因为原始图被Android系统拉伸放到屏幕上,所以这个RectF也需要进行相同的拉伸。
        val originBmpSizeMapMatrix = Matrix()
        originBmpSizeMapMatrix.setScale(originBmpScaleFactor, originBmpScaleFactor)
        originBmpSizeMapMatrix.mapRect(lineRectF)

        //注意移动到中心位置,此时是ImageView的中心位置。
        lineRectF.offset(this.width / 2f - lineRectF.width() / 2f, this.height / 2f - lineRectF.height() / 2f)

        //绘制红色的lineRectF线框。
        val path = Path()
        path.addRoundRect(lineRectF, 20f, 20f, Path.Direction.CW)
        canvas.drawPath(path, mPathPaint!!)

        //绘制黄颜色的中心圆圈。
        mPathPaint!!.color = Color.YELLOW
        mPathPaint!!.strokeWidth = 10f
        canvas.drawCircle(this.width / 2f, this.height / 2f, 50f, mPathPaint!!)


        //选取源bitmap的一块中心区域。SizeF宽高。
        val bmpCenterX = mSrcBmp!!.width / 2f
        val bmpCenterY = mSrcBmp!!.height / 2f

        //规划一片mSrcBmp原图中心区域的RectF
        val bmpRectF = RectF(
            bmpCenterX - mSizeF.width / 2f,
            bmpCenterY - mSizeF.height / 2f,
            bmpCenterX + mSizeF.width / 2f,
            bmpCenterY + mSizeF.height / 2f
        )

        //缩放比例。
        val scaleFactor = 8.5f

        val dst = RectF(0f, 0f, bmpRectF.width() * scaleFactor, bmpRectF.height() * scaleFactor)

        val mx = Matrix()
        mx.setRectToRect(bmpRectF, dst, Matrix.ScaleToFit.CENTER)

        val bmp = Bitmap.createBitmap((mSizeF.width * scaleFactor).toInt(), (mSizeF.height * scaleFactor).toInt(), Bitmap.Config.ARGB_8888)
        val c = Canvas(bmp)
        c.drawColor(Color.BLUE)
        c.drawBitmap(mSrcBmp!!, mx, null)

        testIV?.setImageBitmap(bmp)
    }
}

红色框选框住的区域即为要放大的区域:

https://zhangphil.blog.csdn.net/article/details/135158644https://zhangphil.blog.csdn.net/article/details/135158644

https://zhangphil.blog.csdn.net/article/details/135601993https://zhangphil.blog.csdn.net/article/details/135601993

相关推荐
哲科软件2 小时前
跨平台开发的抉择:Flutter vs 原生安卓(Kotlin)的优劣对比与选型建议
android·flutter·kotlin
jyan_敬言8 小时前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
程序员老刘8 小时前
Android 16开发者全解读
android·flutter·客户端
福柯柯9 小时前
Android ContentProvider的使用
android·contenprovider
不想迷路的小男孩9 小时前
Android Studio 中Palette跟Component Tree面板消失怎么恢复正常
android·ide·android studio
餐桌上的王子9 小时前
Android 构建可管理生命周期的应用(一)
android
菠萝加点糖9 小时前
Android Camera2 + OpenGL离屏渲染示例
android·opengl·camera
用户2018792831679 小时前
🌟 童话:四大Context徽章诞生记
android
yzpyzp10 小时前
Android studio在点击运行按钮时执行过程中输出的compileDebugKotlin 这个任务是由gradle执行的吗
android·gradle·android studio
aningxiaoxixi10 小时前
安卓之service
android