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

相关推荐
TeleostNaCl16 分钟前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang95271 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机
低调小一1 小时前
Swift 语法学习指南 - 与 Kotlin 对比
微信·kotlin·swift
2501_915918412 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
lichong9512 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之dist打包发布在Android工程asserts里
android·vue.js·iphone
Android出海2 小时前
Android 15重磅升级:16KB内存页机制详解与适配指南
android·人工智能·新媒体运营·产品运营·内容运营
一只修仙的猿2 小时前
毕业三年后,我离职了
android·面试
编程乐学3 小时前
安卓非原创--基于Android Studio 实现的新闻App
android·ide·android studio·移动端开发·安卓大作业·新闻app
雅雅姐4 小时前
Android14 init.rc中on boot阶段操作4
android
fatiaozhang95274 小时前
中国移动中兴云电脑W132D-RK3528-2+32G-刷机固件包(非原机制作)
android·xml·电脑·电视盒子·刷机固件·机顶盒刷机