Android画布Canvas绘图scale,Kotlin

Android画布Canvas绘图scale,Kotlin

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:orientation="vertical"
    app:divider="@android:drawable/divider_horizontal_bright"
    app:dividerPadding="5dp"
    app:showDividers="beginning|middle|end">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:background="@drawable/ic_launcher_background"
        android:scaleType="fitCenter"
        android:src="@mipmap/pic" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/ic_launcher_background" />

        <ImageView
            android:id="@+id/iv2"
            android:layout_width="300px"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/ic_launcher_background" />

        <ImageView
            android:id="@+id/iv3"
            android:layout_width="300px"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/ic_launcher_background" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv4"
            android:layout_width="300px"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/ic_launcher_background" />

        <ImageView
            android:id="@+id/iv5"
            android:layout_width="300px"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/ic_launcher_background" />

        <ImageView
            android:id="@+id/iv6"
            android:layout_width="300px"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/ic_launcher_background" />

    </LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
Kotlin 复制代码
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        iv = findViewById(R.id.iv)

        iv1 = findViewById(R.id.iv1)

        lifecycleScope.launch(Dispatchers.Main) {
            delay(500)

            f1()
        }
    }

    private fun f1() {
        val bitmap = ((iv?.drawable as BitmapDrawable).bitmap.copy(Bitmap.Config.ARGB_8888, true))
        //val bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        //canvas.drawColor(Color.LTGRAY) //铺满

        val w = bitmap.width
        val h = bitmap.height
        Log.d("fly", "w=$w h=$h")

        val paint = Paint()
        paint.isAntiAlias = true
        paint.color = Color.RED
        paint.style = Paint.Style.FILL

        val left = 60f
        val top = 60f
        val rectF = RectF(left, top, left + w / 2, top + h / 2)
        canvas.drawRect(rectF, paint)

        canvas.scale(0.5f, 0.5f) //缩小。
        paint.color = Color.YELLOW
        canvas.drawRect(rectF, paint)

        canvas.scale(0.3f, 0.3f) //缩小。
        paint.color = Color.BLUE
        canvas.drawRect(rectF, paint)

        iv1?.setImageBitmap(bitmap)
    }
}

Android画布Canvas绘制drawBitmap基于源Rect和目的Rect,Kotlin-CSDN博客文章浏览阅读471次,点赞9次,收藏8次。文章浏览阅读9.6k次。文章浏览阅读1.8k次。/*Java代码 将Drawable转化为Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth();Android Material Design :LinearLayoutCompat添加分割线divider_linearlayout 分割线-CSDN博客。https://blog.csdn.net/zhangphil/article/details/134818221

相关推荐
JasonYin~43 分钟前
HarmonyOS NEXT 实战之元服务:静态案例效果---手机查看电量
android·华为·harmonyos
zhangphil1 小时前
Android adb查看某个进程的总线程数
android·adb
抛空1 小时前
Android14 - SystemServer进程的启动与工作流程分析
android
Gerry_Liang3 小时前
记一次 Android 高内存排查
android·性能优化·内存泄露·mat
天天打码4 小时前
ThinkPHP项目如何关闭runtime下Log日志文件记录
android·java·javascript
爱数学的程序猿7 小时前
Python入门:6.深入解析Python中的序列
android·服务器·python
brhhh_sehe7 小时前
重生之我在异世界学编程之C语言:深入文件操作篇(下)
android·c语言·网络
zhangphil8 小时前
Android基于Path的addRoundRect,Canvas剪切clipPath简洁的圆形图实现,Kotlin(2)
android·kotlin
Calvin8808288 小时前
Android Studio 的革命性更新:Project Quartz 和 Gemini,开启 AI 开发新时代!
android·人工智能·android studio
敲代码敲到头发茂密9 小时前
【大语言模型】LangChain 核心模块介绍(Memorys)
android·语言模型·langchain