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

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

文章 https://zhangphil.blog.csdn.net/article/details/135980821 实现了基于Matrix缩放Bitmap的动画,但是从左上角(0,0)位置开始的,现在实现从中心点位置开始缩放:

XML 复制代码
<?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:background="@android:color/darker_gray"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@mipmap/mypic" />

    <ImageView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
Kotlin 复制代码
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.RectF
import android.graphics.drawable.BitmapDrawable
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 mSrcImageView: ImageView? = null
    private var result: ImageView? = null

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

    override fun onResume() {
        super.onResume()

        result?.postDelayed({
            val srcBmp = (mSrcImageView?.drawable as BitmapDrawable).bitmap
            matrixAnimScale(srcBmp)
        }, 800)
    }

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

        val deltaW: Float = mSrcImageView!!.width / step
        val deltaH: Float = mSrcImageView!!.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(mSrcImageView!!.width, mSrcImageView!!.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)
                //移动矩阵到中心位置点缩放。
                mx.postTranslate(mSrcImageView!!.width / 2f - dst.width() / 2f, mSrcImageView!!.height / 2f - dst.height() / 2f)

                c.drawBitmap(srcBmp, mx, null)

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

开始从中心点缩放:

逐渐变大:

变大过程:

最终:

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

相关推荐
消失的旧时光-19431 小时前
Android网络框架封装 ---> Retrofit + OkHttp + 协程 + LiveData + 断点续传 + 多线程下载 + 进度框交互
android·网络·retrofit
zcychong1 小时前
Handler(二):Java层源码分析
android
Chef_Chen3 小时前
从0开始学习R语言--Day58--竞争风险模型
android·开发语言·kotlin
用户2018792831674 小时前
演员的智能衣橱系统之Selector选择器
android
CYRUS_STUDIO4 小时前
OLLVM 混淆 + VMP 壳照样破!绕过加壳 SDK 的核心检测逻辑
android·逆向·汇编语言
Kapaseker4 小时前
憋了一周了,12000字深入浅出Android的Context机制
android
betazhou4 小时前
MySQL ROUTER安装部署
android·数据库·mysql·adb·mgr·mysql router
用户2018792831674 小时前
🧙‍♂️ 故事:像素王国的“贴纸工厂”与“圣殿徽章工坊”
android
用户2018792831674 小时前
浅谈Android资源国王的故事
android