Android用setRectToRect实现Bitmap基于Matrix矩阵scale缩放RectF动画,Kotlin(一)

Android用setRectToRect实现Bitmap基于Matrix矩阵scale缩放RectF动画,Kotlin(一)

基于Matrix,控制Bitmap的setRectToRect的目标RectF的宽高。从很小的宽高开始,不断迭代增加setRectToRect的目标RectF的宽高,每次迭代加上一定时延,实现Matrix基础上的动画。

Kotlin 复制代码
import android.graphics.Bitmap
import android.graphics.BitmapFactory
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 kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


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

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

    override fun onResume() {
        super.onResume()

        result?.postDelayed({
            val bmp = BitmapFactory.decodeResource(resources, R.mipmap.mypic)
            matrixAnimScale(bmp, iv!!.width, iv!!.height)
        }, 500)
    }

    private fun matrixAnimScale(srcBmp: Bitmap, width: Int, height: Int) {
        val delayTime = 1L //动画之间的间隔。
        val step = 100f //100次缩放绘制,每步延时delayTime毫秒,总计 delayTime*step 毫秒完成动画。

        val deltaW: Float = width / step
        val deltaH: Float = height / step

        CoroutineScope(Dispatchers.IO).launch {
            var w = 0f
            var h = 0f

            for (i in 0 until step.toInt()) {
                delay(delayTime)

                w += deltaW
                h += deltaH

                val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
                val c = Canvas(bmp)
                c.drawColor(Color.BLUE)

                val src = RectF(0f, 0f, srcBmp.width.toFloat(), srcBmp.height.toFloat())
                val dst = RectF(0f, 0f, w, h)
                val mx = Matrix()
                mx.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER)
                c.drawBitmap(srcBmp, mx, null)

                withContext(Dispatchers.Main) {
                    result?.setImageBitmap(bmp)
                }
            }
        }
    }
}

上下两个ImageView,下面的ImageView展示从小到大不断放大的Bitmap(与上方的ImageView展示的Bitmap一样):

最终动画结束:

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

Android矩阵setRectToRect裁剪Bitmap原图Matrix放大,mapRect标记中心区域,Kotlin-CSDN博客文章浏览阅读180次。【代码】Android矩阵setRectToRect裁剪Bitmap原图Matrix放大,mapRect标记中心区域,Kotlin。https://blog.csdn.net/zhangphil/article/details/135960921

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

相关推荐
小程故事多_807 小时前
从 Tokenizer 到 Next Token,穿透数据流,完整理解 Transformer 底层逻辑
android·深度学习·transformer
三少爷的鞋10 小时前
Android 协程实践:为什么不应将 `supervisorScope` 作为常规方案
android
承渊政道11 小时前
【从零开始大模型开发与微调:基于PyTorch与ChatGLM】(从ChatGLM本地化到LoRA、PEFT的高级微调)
android·人工智能·pytorch·chatglm·本地化部署·大模型开发与微调
倚楼听风语12 小时前
Kotlin 协程与 suspend 原理详解
java·算法·kotlin
2601_9628857213 小时前
如何用 Python 判断一只股票“追涨”还是“抄底”更有效?
android·开发语言·python
素师良码13 小时前
第10篇:屏ESD Check内核逻辑驱动代码完整拆解
android
高晶13 小时前
Android 短信收发模块从零到实战:权限配置、界面设计、发送接收与兼容性适配全攻略
android·程序员
随遇丿而安13 小时前
第18周:ViewModel 基础功能 + 架构优化
android
问心无愧051316 小时前
[GXYCTF2019]Ping Ping Ping
android·笔记
事圆则缓16 小时前
Kotlin 集合与序列:给页面一份清楚的数据
开发语言·windows·kotlin