android 实现在照片上绘制涂鸦的方法
作者:雨幕青山 发布时间:2023-09-17 13:24:49
标签:android,绘制,涂鸦
这个应该是简易版的美图秀秀(小伙伴们吐槽:你这也叫简易版的??我们看着怎么不像啊……)。好吧,只是在图片上绘制涂鸦,然后保存。
一、选择图片
这个道长有必要说一下,在绘制涂鸦时,笔画会根据设置ImageView的大小和屏幕的尺寸(不是像素)产生误差。这个道长暂时还没有找到解决方法,只是规避了一下。
把ImageView设置为全屏,布局文件代码如下
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_draw_pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="选择照片" />
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="保存照片" />
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="擦除笔迹" />
</RelativeLayout>
</FrameLayout>
根据机型设置缩放比例
switch (model){
case "MI 4LTE":
scale = 1.1f;
break;
case "HUAWEI NXT-AL10":
scale = 1.5f;
break;
}
效果图如下
二、绘制涂鸦
实现代码如下:
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch (action) {
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_DOWN:
downX = motionEvent.getX() * scale;
downY = motionEvent.getY() * scale;
break;
case MotionEvent.ACTION_UP:
upX = motionEvent.getX() * scale;
upY = motionEvent.getY() * scale;
canvas.drawLine(downX, downY, upX, upY, paint);
iv_drawpicture.invalidate();
break;
case MotionEvent.ACTION_MOVE:
upX = motionEvent.getX() * scale;
upY = motionEvent.getY() * scale;
canvas.drawLine(downX, downY, upX, upY, paint);
iv_drawpicture.invalidate();
downX = upX;
downY = upY;
break;
default:
break;
}
return true;
}
效果图如下:
三、保存绘制涂鸦后的图片
实现代码如下:
try {
Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream os = getContentResolver().openOutputStream(imageUri);
//compress方法将图片转换成JPG或者PNG格式
drawBitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
Toast.makeText(this, "Saved:" + imageUri.toString(), Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
四、擦除涂鸦笔迹
实现代码如下:
drawBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
canvas = createCanvas(drawBitmap);
paint = createPaint();
canvas.drawBitmap(bmp, 0, 0, paint);
iv_drawpicture.setImageBitmap(drawBitmap);
iv_drawpicture.setOnTouchListener(this);
在照片上绘制涂鸦暂时就到这里,功能比较单一。
来源:https://blog.csdn.net/JJ583500596/article/details/72638595


猜你喜欢
- 作为Android基础组件之一,大家对viewpager已经很熟悉了,网上也有很多使用viewpager来加载图片的案例。但是像微信那样点击
- OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版。本文对OAuth 2.0的
- 我们先回顾下,什么是指针?什么是常量?指针是一种特殊的变量,它里面存储的内容是内存地址。常量是指其里面存储的内容不能发生改变的量。明白了这两
- 新建Rest服务接口:[ServiceContract]public interface IService1{ &nb
- optString方法会在对应的key中的值不存在的时候返回一个空字符串,但是getString会抛一个JSONException 。 /*
- 简介一个APP如果没有页面跳转那么是没有灵魂的,页面跳转的一个常用说法就是Navigator,flutter作为一个最为优秀的前端框架,Na
- 如何实现 WPF 代码查看器控件框架使用.NET40;Visual Studio 2019;代码展示需要使用到AvalonEdit是基于WP
- 占位符Placeholder的使用xml中的配置:<?xml version="1.0" encoding=&qu
- 使用CachePut注解,该方法每次都会执行,会清除对应的key值得缓存(或者更新),分为以下两种情况:如果返回值null,下次进行该key
- java 多线程死锁 相信有过多线程编程经验的朋友,都吃过死锁的苦。除非你不使用多线程,否则死锁的可能性会一直存在。为什么会出现
- 为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平。你的一个决定会影响团队未来的几年。要考虑方面太多:
- 异常的英文单词是exception,字面翻译就是“意外、例外”的意思,也就是非正常情况。事实上,异常本质上是程序上的错误,包括程序逻辑错误和
- 使用了 String 类的 indexOf() 方法在字符串中查找子字符串出现的位置,如过存在返回字符串出现的位置(第一位为0),如果不存在
- 现有一些图片在服务器上的链接,在浏览器中打开这些链接是直接显示在浏览器页面的形式。现在需要生成这些图片的单独下载以及打包下载链接,即在浏览器
- 下面一段内容有项目需求有项目分析,通过一个小demo给大家展示下C#如何对多线程、多任务管理的。项目需求:假设多个任务需要执行,每个任务不是
- 示例代码本文分析示例代码如下:launch(Dispatchers.Main) { flow { em
- 简介这篇文章主要介绍Android用gradle打包,并且调用python脚本将打包好的apk上传到fir.im供相关人员下载,对于学习gr
- 目录卡顿原理卡顿监控ANR原理卡顿原理主线程有耗时操作会导致卡顿,卡顿超过阀值,触发ANR。 应用进程启动时候,Zygote会反射调用Act
- 题目:使用struts2自定义 * ,完成用户登陆才能访问权限的实现在session中存放user变量表示用户登陆,若user为空则用户没有
- 1. 继承1. 子类继承了父类,获得父类的全部Field和方法。子类Student类继承父类,将可以获得父类的全部Field和方法publi