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

相关推荐
曾经的三心草5 小时前
Python2-工具安装使用-anaconda-jupyter-PyCharm-Matplotlib
android·java·服务器
Jerry6 小时前
Compose 设置文字样式
android
飞猿_SIR6 小时前
android定制系统完全解除应用安装限制
android
索迪迈科技7 小时前
影视APP源码 SK影视 安卓+苹果双端APP 反编译详细视频教程+源码
android·影视app源码·sk影视
孔丘闻言7 小时前
python调用mysql
android·python·mysql
萧雾宇9 小时前
Android Compose打造仿现实逼真的烟花特效
android·flutter·kotlin
翻滚丷大头鱼10 小时前
android 性能优化—ANR
android·性能优化
翻滚丷大头鱼10 小时前
android 性能优化—内存泄漏,内存溢出OOM
android·性能优化
拜无忧10 小时前
【教程】flutter常用知识点总结-针对小白
android·flutter·android studio
拜无忧11 小时前
【教程】Flutter 高性能项目架构创建指南:从入门到高性能架构
android·flutter·android studio