Android画布Canvas矩阵Matrix放大裁剪Rect区域的Bitmap,Kotlin

本文主要是介绍Android画布Canvas矩阵Matrix放大裁剪Rect区域的Bitmap,Kotlin,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android画布Canvas矩阵Matrix放大裁剪Rect区域的Bitmap,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)canvas.drawColor(Color.LTGRAY)val paint = Paint(Paint.ANTI_ALIAS_FLAG)paint.color = Color.BLUEpaint.style = Paint.Style.STROKEpaint.strokeWidth = 30fval centerX = originBmp.width / 2val centerY = originBmp.height / 2val w = 300val h = 200val rect = Rect((centerX - w / 2), (centerY - h / 2), (centerX + w / 2), (centerY + h / 2))val rectF = RectF(rect.left.toFloat(), rect.top.toFloat(), rect.right.toFloat(), rect.bottom.toFloat())canvas.clipRect(rect) //选(裁剪)出一块区域。canvas.drawRect(rect, paint) //在这块区域边框绘制线。canvas.drawBitmap(originBmp, null, rect, null)iv1?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))canvas.drawBitmap(originBmp, rect, rectF, null)iv2?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))val factor = 1.8f //放大系数val matrix = Matrix()matrix.setScale(factor, factor)canvas.drawBitmap(originBmp, matrix, null)iv3?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))canvas.translate(centerX - factor * originBmp.width / 2, centerY - factor * originBmp.height / 2)canvas.drawBitmap(originBmp, matrix, null)iv4?.setImageURI(Uri.fromFile(saveBitmapToFile(newBmp)))}private fun saveBitmapToFile(bm: Bitmap): File? {var saveFile: File? = nullval 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}

 

 

 

 

f01ff5004f4c4eb4a513d5f507d2f1bf.png

 

 

 

 

 

 

Android Matrix画布Canvas缩放scale,Kotlin-CSDN博客文章浏览阅读116次,点赞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/135114661

Android画布Canvas绘图scale & translate,Kotlin-CSDN博客文章浏览阅读457次,点赞4次,收藏11次。文章浏览阅读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/134930229Android画布Canvas绘图scale,Kotlin-CSDN博客文章浏览阅读524次,点赞7次,收藏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/134832517Android画布Canvas绘制drawBitmap基于源Rect和目的Rect,Kotlin-CSDN博客文章浏览阅读1.3k次,点赞19次,收藏15次。文章浏览阅读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

 

这篇关于Android画布Canvas矩阵Matrix放大裁剪Rect区域的Bitmap,Kotlin的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/548946

相关文章

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

kotlin的函数forEach示例详解

《kotlin的函数forEach示例详解》在Kotlin中,forEach是一个高阶函数,用于遍历集合中的每个元素并对其执行指定的操作,它的核心特点是简洁、函数式,适用于需要遍历集合且无需返回值的场... 目录一、基本用法1️⃣ 遍历集合2️⃣ 遍历数组3️⃣ 遍历 Map二、与 for 循环的区别三、高

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

kotlin中的数据转换方法(示例详解)

《kotlin中的数据转换方法(示例详解)》这篇文章介绍了Kotlin中将数字转换为字符串和字符串转换为数字的多种方法,包括使用`toString()`、字符串模板、格式化字符串、处理可空类型等,同时... 目录1. 直接使用 toString() 方法2. 字符串模板(自动转换)3. 格式化字符串(控制输

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine