当前位置: 首页 > news >正文

Android画布Canvas绘制drawBitmap基于源Rect和目的Rect,Kotlin

Android画布Canvas绘制drawBitmap基于源Rect和目的Rect,Kotlin

<?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"><ImageViewandroid:id="@+id/iv"android:layout_width="300px"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:background="@drawable/ic_launcher_background"android:scaleType="fitCenter"android:src="@mipmap/pic" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:id="@+id/iv1"android:layout_width="300px"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="@drawable/ic_launcher_background" /><ImageViewandroid:id="@+id/iv2"android:layout_width="300px"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="@drawable/ic_launcher_background" /><ImageViewandroid:id="@+id/iv3"android:layout_width="300px"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="@drawable/ic_launcher_background" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:id="@+id/iv4"android:layout_width="300px"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="@drawable/ic_launcher_background" /><ImageViewandroid:id="@+id/iv5"android:layout_width="300px"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="@drawable/ic_launcher_background" /><ImageViewandroid: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>

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launchclass MainActivity : AppCompatActivity() {private var iv: ImageView? = nullprivate var iv1: ImageView? = nullprivate var iv2: ImageView? = nullprivate var iv3: ImageView? = nullprivate var iv4: ImageView? = nullprivate var iv5: ImageView? = nullprivate var iv6: ImageView? = nulloverride 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)iv3 = findViewById(R.id.iv3)iv4 = findViewById(R.id.iv4)iv5 = findViewById(R.id.iv5)iv6 = findViewById(R.id.iv6)lifecycleScope.launch(Dispatchers.Main) {delay(500)capture1()capture2()capture3()capture4()capture5()capture6()}}private fun capture1() {val srcBmp = (iv!!.drawable as BitmapDrawable).bitmap//创建一个和srcBmp宽高相同背景为Color.LTGRAY空Bitmapval bitmap = Bitmap.createBitmap(srcBmp!!.width, srcBmp!!.height, Bitmap.Config.ARGB_8888);val canvas = Canvas(bitmap)canvas.drawColor(Color.LTGRAY)val dstW = srcBmp!!.width / 4val dstH = srcBmp!!.height / 2val dstRect = Rect(0, 0, dstW, dstH) //bitmap坐标canvas.drawBitmap(srcBmp,null,dstRect,null)iv1!!.setImageBitmap(bitmap)}private fun capture2() {val srcBmp = (iv!!.drawable as BitmapDrawable).bitmapval bitmap = Bitmap.createBitmap(srcBmp!!.width, srcBmp!!.height, Bitmap.Config.ARGB_8888)val canvas = Canvas(bitmap)canvas.drawColor(Color.GREEN)//复制一个宽1/2和高1/2的原图。val dstW = srcBmp!!.width / 2val dstH = srcBmp!!.height / 2//框住原图中心点右下1/4部分。val srcRect = Rect(dstW, dstH, srcBmp!!.width, srcBmp!!.height)//bitmap左上角偏移20开始摆放。val dstRect = Rect(20, 20, dstW + 20, dstH + 20)canvas.drawBitmap(srcBmp,srcRect,dstRect,null)iv2!!.setImageBitmap(bitmap)}private fun capture3() {val srcBmp = (iv!!.drawable as BitmapDrawable).bitmapval bitmap = Bitmap.createBitmap(srcBmp!!.width, srcBmp!!.height, Bitmap.Config.ARGB_8888)val canvas = Canvas(bitmap)canvas.drawColor(Color.CYAN)val dstW = srcBmp!!.width / 2val dstH = srcBmp!!.height / 2val centerX = srcBmp!!.width / 2val centerY = srcBmp!!.height / 2//框住原图右上1/4部分,坐标仍以原图为准。val srcRect = Rect(centerX, 0, centerX + srcBmp!!.width / 2, centerY)//摆放到bitmap的中心位置。val dstRect = Rect(centerX - dstW / 2, centerY - dstH / 2, centerX + dstW / 2, centerY + dstH / 2)canvas.drawBitmap(srcBmp,srcRect,dstRect,null)iv3!!.setImageBitmap(bitmap)}private fun capture4() {val srcBmp = (iv!!.drawable as BitmapDrawable).bitmapval w = srcBmp!!.width / 2val h = srcBmp!!.height / 2val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)val canvas = Canvas(bitmap)canvas.drawColor(Color.YELLOW)val centerX = srcBmp!!.width / 2val centerY = srcBmp!!.height / 2//框住原图中心1/2部分,坐标仍以原图为准。val srcRect = Rect(centerX - w / 2, centerY - h / 2, centerX + w / 2, centerY + h / 2)//直接摆放到bitmap。val dstRect = Rect(0, 0, w, h)canvas.drawBitmap(srcBmp,srcRect,dstRect,null)iv4!!.setImageBitmap(bitmap)}//截取原图右下1/3的部分private fun capture5() {val srcBmp = (iv!!.drawable as BitmapDrawable).bitmapval w = srcBmp!!.width / 3val h = srcBmp!!.height / 3val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)val canvas = Canvas(bitmap)canvas.drawColor(Color.RED)val left = (srcBmp!!.width / 3) * 2val top = (srcBmp!!.height / 3) * 2//框住原图右下1/3部分,坐标仍以原图为准val srcRect = Rect(left, top, left + w, top + h)//直接摆放到bitmap。val dstRect = Rect(0, 0, w, h)canvas.drawBitmap(srcBmp,srcRect,dstRect,null)iv5!!.setImageBitmap(bitmap)}//截取原图左侧中间1/3部分private fun capture6() {val srcBmp = (iv!!.drawable as BitmapDrawable).bitmapval w = srcBmp!!.width / 3val h = srcBmp!!.height / 3val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)val canvas = Canvas(bitmap)canvas.drawColor(Color.BLUE)val top = srcBmp!!.height / 3//框住原图部分。val srcRect = Rect(0, top, 0 + w, top + h)//直接摆放到bitmap。val dstRect = Rect(0, 0, w, h)canvas.drawBitmap(srcBmp,srcRect,dstRect,null)iv6!!.setImageBitmap(bitmap)}
}

Android Drawable 转化成 Bitmap-CSDN博客文章浏览阅读1.8k次。/*Java代码 将Drawable转化为Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap bitmaphttps://blog.csdn.net/zhangphil/article/details/43767535

Android Material Design :LinearLayoutCompat添加分割线divider_linearlayout 分割线-CSDN博客文章浏览阅读9.6k次。Android Material Design :LinearLayoutCompat添加分割线dividerAndroid Material Design 扩展支持包中的LinearLayoutCompat是过去的LinearLayout的扩展,可以为此布局中功德子View之间添加分割线divider。其中比较关键的地方有两点:(1)app:showDividers="beg_linearlayout 分割线https://blog.csdn.net/zhangphil/article/details/48899585

相关文章:

Android画布Canvas绘制drawBitmap基于源Rect和目的Rect,Kotlin

Android画布Canvas绘制drawBitmap基于源Rect和目的Rect&#xff0c;Kotlin <?xml version"1.0" encoding"utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat xmlns:android"http://schemas.android.com/apk/res/android"xmlns…...

深度优先搜索LeetCode979. 在二叉树中分配硬币

给你一个有 n 个结点的二叉树的根结点 root &#xff0c;其中树中每个结点 node 都对应有 node.val 枚硬币。整棵树上一共有 n 枚硬币。 在一次移动中&#xff0c;我们可以选择两个相邻的结点&#xff0c;然后将一枚硬币从其中一个结点移动到另一个结点。移动可以是从父结点到…...

C++学习之路(十)C++ 用Qt5实现一个工具箱(增加一个时间戳转换功能)- 示例代码拆分讲解

上篇文章&#xff0c;我们用 Qt5 实现了在小工具箱中添加了《JSON数据格式化》功能&#xff0c;还是比较实用的。为了继续丰富我们的工具箱&#xff0c;今天我们就再增加一个平时经常用到的功能吧&#xff0c;就是「 时间戳转换 」功能&#xff0c;而且实现点击按钮后文字进行变…...

Linux 5.15安全特性之ARM64 PAC

ARM64 PAC&#xff08;Pointer Authentication Code&#xff09;机制是ARM架构中引入的一种安全特性&#xff0c;旨在提供指针的完整性和安全性保护。它通过在指针中插入一段额外的代码进行签名&#xff0c;以验证指针的完整性&#xff0c;从而抵御缓冲区溢出和代码注入等攻击。…...

同旺科技 分布式数字温度传感器

内附链接 1、数字温度传感器 主要特性有&#xff1a; ● 支持PT100 / PT1000 两种铂电阻&#xff1b; ● 支持 2线 / 3线 / 4线 制接线方式&#xff1b; ● 支持5V&#xff5e;17V DC电源供电&#xff1b; ● 支持电源反接保护&#xff1b; ● 支持通讯波特率1200bps、2…...

状态空间的定义

状态空间是描述一个系统所有可能状态的集合。在系统理论、控制论、计算机科学、强化学习等领域&#xff0c;状态空间是一种常见的概念。 状态空间框架是一种用于描述和分析系统的方法&#xff0c;它包括系统的状态、状态之间的转移关系以及与状态相关的行为。下面详细解释状态…...

数据挖掘实战-基于word2vec的短文本情感分析

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…...

大数据面试总结

1、冒泡排序、选择排序 2、二分查找 3、 hashmap和hashtable的区别&#xff1f;hashmap的底层实现原理&#xff1f; a、hashtable和hashmap的区别&#xff1a; 1、hashtable是线程安全的&#xff0c;会在每一个方法中都添加方法synchronize&#xff08;同步机制&#xff09…...

利大于弊:物联网技术对电子商务渠道的影响

For Better or For Worse: Impacts of IoT Technology in e-Commerce Channel 物联网技术使用传感器和其他联网设备来手机和共享数据&#xff0c;并且被视为一种可以为供应链成员带来巨大的机会的突破性技术。本文的研究背景是&#xff1a;一个提供物联网基础设备的电子商务平…...

Python 元组详解(tuple)

文章目录 1 概述1.1 性质1.2 下标1.3 切片 2 常用方法2.1 访问&#xff1a;迭代、根据下标2.2 删除&#xff1a;del2.3 运算符&#xff1a;、*2.4 计算元组中元素个数&#xff1a;len()2.5 返回元组中元素最大值&#xff1a;max()2.6 返回元组中元素最小值&#xff1a;min()2.7…...

Redis部署-主从模式

目录 单点问题 主从模式 解析主从模式 配置redis主从模式 info replication命令查看复制相关的状态 断开复制关系 安全性 只读 传输延迟 拓扑结构 数据同步psync replicationid offset psync运行流程 全量复制流程 无硬盘模式 部分复制流程 积压缓冲区 实时复…...

全栈冲刺 之 一天速成MySQL

一、为什么使用数据库 数据储存在哪里&#xff1f; 硬盘、网盘、U盘、光盘、内存&#xff08;临时存储&#xff09; 数据持久化 使用文件来进行存储&#xff0c;数据库也是一种文件&#xff0c;像excel &#xff0c;xml 这些都可以进行数据的存储&#xff0c;但大量数据操作…...

服务器运行train.py报错解决

在服务器配置完虚拟环境以及安装完各种所需库后&#xff0c;发现报错Traceback (most recent call last): File "/root/yolov5-master/yolov5-master/train.py", line 48, in <module> import val as validate # for end-of-epoch mAP File "/root/yolov5…...

Flutter开发type ‘Future<int>‘ is not a subtype of type ‘int‘ in type cast错误

文章目录 问题描述错误源码 问题分析解决方法修改后的代码 问题描述 今天有个同事调试flutter程序时报错&#xff0c;问我怎么解决&#xff0c;程序运行时报如下错误&#xff1a; type ‘Future’ is not a subtype of type ‘int’ in type cast 错误源码 int order Databas…...

Nginx(十二) gzip gzip_static sendfile directio aio 组合使用测试(2)

测试10&#xff1a;开启gzip、sendfile、aio、directio1m&#xff0c;关闭gzip_static&#xff0c;请求/index.js {"time_iso8601":"2023-11-30T17:20:5508:00","request_uri":"/index.js","status":"200","…...

hls实现播放m3u8视频将视频流进行切片 HLS.js简介

github官网GitHub - video-dev/hls.js: HLS.js is a JavaScript library that plays HLS in browsers with support for MSE.HLS.js is a JavaScript library that plays HLS in browsers with support for MSE. - GitHub - video-dev/hls.js: HLS.js is a JavaScript library …...

Ubuntu20.04部署TVM流程及编译优化模型示例

前言&#xff1a;记录自己安装TVM的流程&#xff0c;以及一个简单的利用TVM编译模型并执行的示例。 1&#xff0c;官网下载TVM源码 git clone --recursive https://github.com/apache/tvmgit submodule init git submodule update顺便完成准备工作&#xff0c;比如升级cmake版本…...

华为OD机试真题-两个字符串间的最短路径问题-2023年OD统一考试(C卷)

题目描述: 给定两个字符串,分别为字符串A与字符串B。例如A字符串为ABCABBA,B字符串为CBABAC可以得到下图m*n的二维数组,定义原点为(0, 0),终点为(m, n),水平与垂直的每一条边距离为1,映射成坐标系如下图。 从原点(0, 0)到(0, A)为水平边,距离为1,从(0, A)到(A, C)为垂…...

python try-except

相比于直接raise ValueError&#xff0c;使用try-except可以使程序在发生异常后仍然能够运行。 在try的部分中&#xff0c;当遇到第一个Error&#xff0c;就跳转到except中寻找对应类型的error&#xff0c;后续代码不再执行&#xff0c;如果try中有多个Error&#xff0c;注意顺…...

flutter开发实战-ValueListenableBuilder实现局部刷新功能

flutter开发实战-ValueListenableBuilder实现局部刷新功能 在创建的新工程中&#xff0c;点击按钮更新counter后&#xff0c;通过setState可以出发本类的build方法进行更新。当我们只需要更新一小部分控件的时候&#xff0c;通过setState就不太合适了&#xff0c;这就需要进行…...

AI Agent与Agentic AI:原理、应用、挑战与未来展望

文章目录 一、引言二、AI Agent与Agentic AI的兴起2.1 技术契机与生态成熟2.2 Agent的定义与特征2.3 Agent的发展历程 三、AI Agent的核心技术栈解密3.1 感知模块代码示例&#xff1a;使用Python和OpenCV进行图像识别 3.2 认知与决策模块代码示例&#xff1a;使用OpenAI GPT-3进…...

JavaScript 中的 ES|QL:利用 Apache Arrow 工具

作者&#xff1a;来自 Elastic Jeffrey Rengifo 学习如何将 ES|QL 与 JavaScript 的 Apache Arrow 客户端工具一起使用。 想获得 Elastic 认证吗&#xff1f;了解下一期 Elasticsearch Engineer 培训的时间吧&#xff01; Elasticsearch 拥有众多新功能&#xff0c;助你为自己…...

从零实现富文本编辑器#5-编辑器选区模型的状态结构表达

先前我们总结了浏览器选区模型的交互策略&#xff0c;并且实现了基本的选区操作&#xff0c;还调研了自绘选区的实现。那么相对的&#xff0c;我们还需要设计编辑器的选区表达&#xff0c;也可以称为模型选区。编辑器中应用变更时的操作范围&#xff0c;就是以模型选区为基准来…...

visual studio 2022更改主题为深色

visual studio 2022更改主题为深色 点击visual studio 上方的 工具-> 选项 在选项窗口中&#xff0c;选择 环境 -> 常规 &#xff0c;将其中的颜色主题改成深色 点击确定&#xff0c;更改完成...

蓝牙 BLE 扫描面试题大全(2):进阶面试题与实战演练

前文覆盖了 BLE 扫描的基础概念与经典问题蓝牙 BLE 扫描面试题大全(1)&#xff1a;从基础到实战的深度解析-CSDN博客&#xff0c;但实际面试中&#xff0c;企业更关注候选人对复杂场景的应对能力&#xff08;如多设备并发扫描、低功耗与高发现率的平衡&#xff09;和前沿技术的…...

Golang dig框架与GraphQL的完美结合

将 Go 的 Dig 依赖注入框架与 GraphQL 结合使用&#xff0c;可以显著提升应用程序的可维护性、可测试性以及灵活性。 Dig 是一个强大的依赖注入容器&#xff0c;能够帮助开发者更好地管理复杂的依赖关系&#xff0c;而 GraphQL 则是一种用于 API 的查询语言&#xff0c;能够提…...

第25节 Node.js 断言测试

Node.js的assert模块主要用于编写程序的单元测试时使用&#xff0c;通过断言可以提早发现和排查出错误。 稳定性: 5 - 锁定 这个模块可用于应用的单元测试&#xff0c;通过 require(assert) 可以使用这个模块。 assert.fail(actual, expected, message, operator) 使用参数…...

涂鸦T5AI手搓语音、emoji、otto机器人从入门到实战

“&#x1f916;手搓TuyaAI语音指令 &#x1f60d;秒变表情包大师&#xff0c;让萌系Otto机器人&#x1f525;玩出智能新花样&#xff01;开整&#xff01;” &#x1f916; Otto机器人 → 直接点明主体 手搓TuyaAI语音 → 强调 自主编程/自定义 语音控制&#xff08;TuyaAI…...

C++使用 new 来创建动态数组

问题&#xff1a; 不能使用变量定义数组大小 原因&#xff1a; 这是因为数组在内存中是连续存储的&#xff0c;编译器需要在编译阶段就确定数组的大小&#xff0c;以便正确地分配内存空间。如果允许使用变量来定义数组的大小&#xff0c;那么编译器就无法在编译时确定数组的大…...

【Go语言基础【12】】指针:声明、取地址、解引用

文章目录 零、概述&#xff1a;指针 vs. 引用&#xff08;类比其他语言&#xff09;一、指针基础概念二、指针声明与初始化三、指针操作符1. &&#xff1a;取地址&#xff08;拿到内存地址&#xff09;2. *&#xff1a;解引用&#xff08;拿到值&#xff09; 四、空指针&am…...