Android Canvas画布saveLayer与对应restoreToCount,Kotlin

Android Canvas画布saveLayer与对应restoreToCount,Kotlin

Kotlin 复制代码
    private fun mydraw() {
        val originBmp = BitmapFactory.decodeResource(resources, R.mipmap.pic).copy(Bitmap.Config.ARGB_8888, true)

        val newBmp = Bitmap.createBitmap(originBmp.width, originBmp.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(newBmp)

        //把原图绘制在画布Canvas
        canvas.drawBitmap(originBmp, 0f, 0f, null)
        iv?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))

        val paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint.color = Color.RED
        paint.textSize = 40f
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 2f
        paint.textAlign = Paint.Align.LEFT


        //新图层,绿色背景,红色圆圈。
        val layer1 = canvas.saveLayer(0f, 0f, canvas.width.toFloat(), (canvas.height.toFloat() * 0.9).toFloat(), null)
        canvas.drawColor(Color.GREEN)
        canvas.drawCircle((canvas.width / 2).toFloat(), (canvas.height / 2).toFloat(), 50f, paint)
        canvas.restoreToCount(layer1)
        iv1?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))

        //新图层,灰色背景,直线。
        val layer2 = canvas.saveLayer(0f, 0f, canvas.width.toFloat(), (canvas.height.toFloat() * 0.8).toFloat(), null)
        canvas.drawColor(Color.GRAY)
        canvas.drawLine(
            canvas.width.toFloat() / 2,
            canvas.height.toFloat() / 2,
            canvas.width.toFloat(),
            canvas.height.toFloat(),
            paint
        )
        canvas.restoreToCount(layer2)
        iv2?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))

        //新图层,绿色背景,文字fly
        val layer3 = canvas.saveLayer(0f, 0f, canvas.width.toFloat(), (canvas.height.toFloat() * 0.5).toFloat(), null)
        canvas.drawColor(Color.BLUE)
        val x = 50f
        val y = 150f
        canvas.drawText("fly", x, y, paint)
        canvas.restoreToCount(layer3)
        iv3?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))

        /**
         * 如果不事先把bitmap通过文件形式存放好,再通过setImageURI设置到ImageView,那么就像
         * iv4?.setImageBitmap(newBmp),设置的newBmp是最终的绘图结果,saveLayer与restoreToCount操作的是在内存的newBmp,
         * 即便是ivx?.setImageURI后,newBmp内存数据变化,最终都是iv4里面的显示。为了细致观察这种区别,才先保存成不变的文件,再读文件加载出来观察。
         *
         * 注意,因为restoreToCount了,图层叠加到原图上,所以显示的结果是最后面的叠加到最前面的,把最前面的遮住了。
         * 如果不restoreToCount,则不会叠加,因为saveLayer创建了一个透明图层,是在原图之上的绘制。
         */


        iv4?.setImageBitmap(newBmp)
    }
    
    private fun saveBitmapToFile(bm: Bitmap): File? {
        var saveFile: File? = null
        val savePath =
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()
        if (!Files.exists(Paths.get(savePath))) {
            Log.d("保存文件", "${savePath}不存在!")
        } else {
            saveFile = File(savePath, System.currentTimeMillis().toString() + ".jpeg")
            try {
                val saveImgOut = FileOutputStream(saveFile)
                //压缩
                bm.compress(Bitmap.CompressFormat.JPEG, 90, saveImgOut)
                saveImgOut.flush()
                saveImgOut.close()
                Log.d("保存文件", "Bitmap保存至 ${saveFile.absoluteFile.toPath()}")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        return saveFile
    }

如果把

Kotlin 复制代码
canvas.restoreToCount()

全部注释掉,则为

Android Canvas状态save与restore,Kotlin-CSDN博客文章浏览阅读218次,点赞3次,收藏3次。文章浏览阅读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/135113616

相关推荐
ljt272496066113 分钟前
Compose笔记(五十八)--LinearOutSlowInEasing
android·笔记·android jetpack
q***484116 分钟前
Redis Desktop Manager(Redis可视化工具)安装及使用详细教程
android·前端·后端
renxhui37 分钟前
Dart 速通攻略(面向 Android 工程师)
android·flutter·dart
Frank_HarmonyOS38 分钟前
MVI模式
android
m***98240 分钟前
Redis6.2.6下载和安装
android·前端·后端
未来之窗软件服务1 小时前
幽冥大陆(三十九)php二维数组去重——东方仙盟筑基期
android·开发语言·算法·php·仙盟创梦ide·东方仙盟·东方仙盟sdk
w***4242 小时前
【mysql部署】在ubuntu22.04上安装和配置mysql教程
android·mysql·adb
e***75392 小时前
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
android·sql·mysql
道路与代码之旅2 小时前
“变量也能是函数?——论 Kotlin 中那些会说话的变量,以及它们如何让代码少说废话”
android·开发语言·kotlin
x***01062 小时前
Mysql之主从复制
android·数据库·mysql