Android画布Canvas绘图scale & translate,Kotlin

Android画布Canvas绘图scale & translate,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="wrap_content"
            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/iv3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/ic_launcher_background" />

        <ImageView
            android:id="@+id/iv4"
            android:layout_width="wrap_content"
            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.drawable.BitmapDrawable
import android.os.Bundle
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
    private var iv2: 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)
        iv2 = findViewById(R.id.iv2)

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

            f1()
            f2()
        }
    }

    private fun f1() {
        val bitmap = ((iv?.drawable as BitmapDrawable).bitmap.copy(Bitmap.Config.ARGB_8888, true))
        val canvas = Canvas(bitmap)

        val left = 50f
        val top = 200f

        canvas.scale(0.5f, 0.3f)
        canvas.translate(left, top)
        canvas.drawBitmap(bitmap, left, top, null)

        iv1?.setImageBitmap(bitmap)
    }

    private fun f2() {
        val bitmap = ((iv?.drawable as BitmapDrawable).bitmap.copy(Bitmap.Config.ARGB_8888, true))
        val canvas = Canvas(bitmap)

        val left = 50f
        val top = 200f

        canvas.scale(0.8f, 0.2f)
        canvas.translate(left, top)
        canvas.drawBitmap(bitmap, left, top, null)

        iv2?.setImageBitmap(bitmap)
    }
}

Android画布Canvas绘图scale,Kotlin-CSDN博客文章浏览阅读237次,点赞2次,收藏2次。文章浏览阅读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/134832517

相关推荐
liang_jy1 小时前
Android 窗口显示(一)—— Activity、Window 和 View 之间的联系
android·面试
用户2018792831671 小时前
快递分拣中心里的 LinkedList 冒险:从源码到实战的趣味解析
android
玲小珑2 小时前
Auto.js 入门指南(十五)脚本加密与安全防护
android·前端
花开月满西楼2 小时前
Android实例项目【智能家居系统】实现数据库登录注册+动画效果+网页跳转+短信发送!!!
android·数据库·智能家居
----云烟----3 小时前
使用WinUSB读写USB设备
android·智能手机·android studio
weixin_446938873 小时前
android stdio 关闭所有真机
android
bryant_meng4 小时前
【linux】Linux vs Android
android·linux·运维
不会写代码的猴子5 小时前
AndroidStudio下载gradle依赖很慢的解决方法之一
android·android studio
我命由我123455 小时前
Android 开发问题:CardView 的阴影效果会受到父容器的裁切
android·java·开发语言·java-ee·android studio·android-studio·android runtime
Coffeeee5 小时前
现在的需求这么花哨了吗,文本都能拼上自定义组件啦?
android·面试·kotlin