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

相关推荐
ANYOUZHEN25 分钟前
bugku shell
android
南宫码农3 小时前
我的电视 - Android原生电视直播软件 完整使用教程
android·开发语言·windows·电视盒子
道亦无名4 小时前
音频数据特征值提取 方法和步骤
android·音视频
Lancker4 小时前
定制侠 一个国产纯血鸿蒙APP的诞生过程
android·华为·智能手机·鸿蒙·国产操作系统·纯血鸿蒙·华为鸿蒙
2601_949809595 小时前
flutter_for_openharmony家庭相册app实战+通知设置实现
android·javascript·flutter
液态不合群6 小时前
【面试题】MySQL 中 count(*)、count(1) 和 count(字段名) 有什么区别?
android·数据库·mysql
雪球Snowball7 小时前
【Android关键流程】资源加载
android
2501_915918418 小时前
常见 iOS 抓包工具的使用,从代理抓包、设备抓包到数据流抓包
android·ios·小程序·https·uni-app·iphone·webview
灯火不休ᝰ9 小时前
[kotlin] 从Java到Kotlin:掌握基础语法差异的跃迁指南
java·kotlin·安卓
墨月白9 小时前
[QT]QProcess的相关使用
android·开发语言·qt