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

相关推荐
csj501 小时前
安卓基础之《(6)—Activity组件(3)》
android
怀旧,1 小时前
【Linux系统编程】13. Ext系列⽂件系统
android·linux·缓存
Dabei1 小时前
Android 语音助手简单实现与语音助手“执行任务”交流
android·前端
jzlhll1231 小时前
android NDSDManager onResolveFailed errorCode=3的解决方案
android
芦半山2 小时前
四年之后,重新审视 MTE:从硬件架构到工程落地
android·安全
2501_916007472 小时前
iOS与Android符号还原服务统一重构实践总结
android·ios·小程序·重构·uni-app·iphone·webview
allk552 小时前
Android 屏幕适配全维深度解析
android·性能优化·界面适配
Android系统攻城狮2 小时前
Android ALSA驱动进阶之获取采样格式位宽snd_pcm_format_width:用法实例(九十八)
android·pcm·音频进阶·alsa驱动
莫比乌斯环3 小时前
【日常随笔】Android 跳离行为分析 - Instrumentation
android·架构·代码规范
aningxiaoxixi3 小时前
android 媒体之 MediaSession
android·媒体