Android 图片编辑(彩色划线、添加文字、单步撤销等功能实现)
添加要编辑图片的URL;关于URL的获取这边的例子是截屏图片:
getWindowBitmapPath()方法内容请移步
Android 截屏并保存到本地(兼容Android 10.0)_深海呐的博客-CSDN博客
R.id.screenshot -> {//截屏Toast.makeText(activity, "截图成功!已保存到相册", Toast.LENGTH_SHORT).show()val windowBitmapPath = getWindowBitmapPath()startSetImg(windowBitmapPath)}
操作按钮初始化(含部分必要逻辑)
fun startSetImg(url: Uri?) {findViewById<View>(R.id.screenshot_layout).visibility = View.VISIBLEvar edit = findViewById<EditText>(R.id.screenshot_mat_edit)val mat = findViewById<MouseMat>(R.id.screenshot_mat)mat.setImageURI(url)findViewById<View>(R.id.screenshot_mat_blue).setOnClickListener {mat.setColor(Color.BLUE)}findViewById<View>(R.id.screenshot_mat_red).setOnClickListener {mat.setColor(Color.RED)}findViewById<View>(R.id.screenshot_mat_green).setOnClickListener {mat.setColor(Color.GREEN)}findViewById<View>(R.id.screenshot_mat_back).setOnClickListener {mat.back()}findViewById<View>(R.id.screenshot_mat_delete).setOnClickListener {mat.reset()}findViewById<View>(R.id.screenshot_mat_text).setOnClickListener {if (edit.text.isNotEmpty())mat.addText(edit.text.toString())}findViewById<View>(R.id.screenshot_mat_text_back).setOnClickListener {mat.backText()}findViewById<View>(R.id.screenshot_mat_save).setOnClickListener {val view = mat //findViewById<View>(R.id.screenshot_layout)val bitmap: Bitmap = Bitmap.createBitmap(view?.width ?: 0, view?.height ?: 0,Bitmap.Config.ARGB_8888)view?.draw(Canvas(bitmap))ImageSaveUtil.saveAlbum(this, bitmap, Bitmap.CompressFormat.JPEG, 80, true)mat.reset()findViewById<View>(R.id.screenshot_layout).visibility = View.GONE}}
布局
<FrameLayoutandroid:id="@+id/screenshot_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="gone"><TextViewandroid:id="@+id/screenshot_background"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#aa112233"android:visibility="visible" /><com.ys.bdtp.adm.utils.MouseMatandroid:id="@+id/screenshot_mat"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="60dp"android:background="#00000000"android:visibility="visible" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:gravity="center"android:orientation="horizontal"><TextViewandroid:id="@+id/screenshot_mat_red"android:layout_width="40dp"android:layout_height="30dp"android:layout_marginRight="12dp"android:background="#ff0000"android:padding="6dp" /><TextViewandroid:id="@+id/screenshot_mat_blue"android:layout_width="40dp"android:layout_height="30dp"android:layout_marginRight="12dp"android:background="#0000ff"android:padding="6dp" /><TextViewandroid:id="@+id/screenshot_mat_green"android:layout_width="40dp"android:layout_height="30dp"android:layout_marginRight="12dp"android:background="#00ff00"android:padding="6dp" /><Buttonandroid:id="@+id/screenshot_mat_back"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="6dp"android:padding="6dp"android:text="删线" /><Buttonandroid:id="@+id/screenshot_mat_text_back"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="6dp"android:padding="6dp"android:text="删字" /><Buttonandroid:id="@+id/screenshot_mat_delete"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="6dp"android:padding="6dp"android:text="还原" /><Buttonandroid:id="@+id/screenshot_mat_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="6dp"android:padding="6dp"android:text="添字" /><EditTextandroid:id="@+id/screenshot_mat_edit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="6dp"android:hint="输入添加的文字"android:padding="6dp" /><Buttonandroid:id="@+id/screenshot_mat_save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="6dp"android:padding="6dp"android:text="保存" /></LinearLayout></FrameLayout>
画板实体类
/*** Author:XingHai.Zhao* Purpose:画板*/
public class MouseMat extends androidx.appcompat.widget.AppCompatImageView {private boolean isText;private String thisText;class TextBean {public TextBean(String text, float x, float y, Paint paint) {this.text = text;this.x = x;this.y = y;this.paint = paint;}String text = "";float x;float y;Paint paint;}public MouseMat(Context context) {super(context);}public MouseMat(Context context, AttributeSet attrs) {super(context, attrs);init();setClickable(true);//设置可点击}public MouseMat(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();setClickable(true);//设置可点击}private List<Path> pathList = new ArrayList<>();private List<Paint> paintList = new ArrayList<>();private List<TextBean> textList = new ArrayList<>();private int thisColor = Color.RED;private void init() {path = new Path();paint = new Paint();paint.setColor(thisColor);//颜色paint.setStyle(Paint.Style.STROKE);paint.setTypeface(Typeface.DEFAULT);paint.setAntiAlias(true);paint.setTextSize(40);paint.setStrokeWidth(2); //边框宽度}public MouseTouchListener mouseTouchListener;public interface MouseTouchListener {void MouseEvent(MotionEvent event);}public void setMouseTouchListener(MouseTouchListener mouseTouchListener) {this.mouseTouchListener = mouseTouchListener;}Path path = new Path();Paint paint = new Paint();@SuppressLint("ClickableViewAccessibility")@Overridepublic boolean onTouchEvent(MotionEvent event) {if (!isText) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:init();path.moveTo(event.getX(), event.getY());break;case MotionEvent.ACTION_MOVE:path.lineTo(event.getX(), event.getY());invalidate();break;case MotionEvent.ACTION_UP:pathList.add(0, path);paintList.add(0, paint);invalidate();break;}} else {//文字添加模式if (event.getAction() == MotionEvent.ACTION_UP) {if (thisText != null) {textList.add(0, new TextBean(thisText, event.getX(), event.getY(),paint));invalidate();}isText = false;}}if (mouseTouchListener != null)mouseTouchListener.MouseEvent(event);return super.onTouchEvent(event);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);for (int i = 0; i < pathList.size(); i++) {try {canvas.drawPath(pathList.get(i), paintList.get(i));} catch (Exception e) {e.printStackTrace();}}for (int i = 0; i < textList.size(); i++) {try {canvas.drawText(textList.get(i).text, textList.get(i).x,textList.get(i).y, textList.get(i).paint);} catch (Exception e) {e.printStackTrace();}}}/*** Author:XingHai.Zhao* Purpose: 清除内容*/public void reset() {pathList.clear();textList.clear();invalidate();}public void back() {if (paintList.size() > 0) {try {pathList.remove(0);} catch (Exception e) {e.printStackTrace();}invalidate();}}public void backText() {if (textList.size() > 0) {try {textList.remove(0);} catch (Exception e) {e.printStackTrace();}invalidate();}}/*** Author:XingHai.Zhao* Purpose: 设置颜色*/public void setColor(int color) {this.thisColor = color;}public void addText(String text) {thisText = text;isText = true;}
}
Android 图片编辑(彩色划线、添加文字、单步撤销等功能实现)相关推荐
- android 图片上动态添加文字,摘抄 android图片中添加文字水印
* 添加文字到图片,类似水印文字. * @param gContext * @param gResId * @param gText * @return */ public static Bitmap ...
- android 5.1中添加来电翻转静音的功能
网上关于来电翻转静音的功能有很多实现,但大同小以异,下面主要记录以下几点: 1.settting中添加控制. 2.Dialer中添加具体的功能实现. 3.AudioManager实现完成静音. 首先, ...
- android图片编辑加文字,图片编辑加字下载-图片编辑加字 安卓版v3.9.0.0406-PC6安卓网...
图片编辑加字app是一款手机图片编辑加字软件.图片编辑加字软件支持给图片添加各种样式的文字,自由排版,还能制作长图,非常实用. 软件介绍 图片编辑加字app是一款功能强大的多图片添加文字软件.可以添加 ...
- WPS添加下划线,文字尾部不显示下划线问题解决(一个So stupid问题)
记录一个傻瓜操作,嗯,更想删WPS了. 一.问题如下 首先如图: 选择wps中的下划线操作 理想中他应该是这样的: 选中的内容应该在下划线中间,是吧,默认正常操作就应该这样. 实际上它出来的效果是这样 ...
- css里给文字加下划线代码,css添加文字下划线样式的方法
css添加文字下划线样式的方法 发布时间:2020-08-31 13:54:27 来源:亿速云 阅读:65 作者:小新 这篇文章将为大家详细讲解有关css添加文字下划线样式的方法,小编觉得挺实用的,因 ...
- php css下划线,css如何添加文字下划线样式?(代码详解)
css如何添加文字下划线样式?本篇文章就给大家介绍css添加文字下划线样式的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助. 首先我们来了解一下css添加文字下划线样式的方法有哪 ...
- android图片上水印字体颜色,Android给图片添加文字和水印
话不多说 上图 gif5新文件.gif public class ImageUtil { /** * 设置水印图片在左上角 * * @param context 上下文 * @param src * ...
- android水印控件,Android图片添加文字水印并保存水印文字图片到指定文件
Android图片添加文字水印并保存水印文字图片到指定文件package zhangphil.test;import android.graphics.Bitmap;import android.gr ...
- fabricJs 在vue项目中的实战记录(四)添加文字以及文字的操作[字体大小、颜色、字间距、行间距、透明度、加粗、斜体、下划线、删除线、对齐方式、字体设置]
在阅读以下文章的基础上查看本篇笔记: 第一篇:fabricJs 在vue项目中的实战记录(一)引入以及初始化 第二篇:fabricJs 在vue项目中的实战记录(二)fabricjs设置海报背景(图片 ...
最新文章
- mmdetection2.3.0版本安装过程,以及训练、测试、可视化等(亲测好用,很顺利)
- nio java 内核拷贝_大文件拷贝,试试NIO的内存映射
- linux 无线网卡休眠,无线网卡在Linux下活起来
- HP LaserJet 1010卡纸解决方法
- React Native 开发环境搭建
- Linux cpuidle framework
- “macOS Catalina下TeXstudio内置PDF阅读器无法正常显示中文”的解决办法
- SQL Server游标
- 集邦咨询:预估今年GaN功率元件营收达8300万美元
- vue脚手架实现选项卡_从零一步步实现一个前端脚手架
- 七、JVM类加载机制
- 为什么RISC-V在中国岌岌可危?
- 银河麒麟 安装PL2303GC USB转串口驱动
- 《信息物理融合系统(CPS)设计、建模与仿真——基于 Ptolemy II 平台》——第3章 数据流 3.1同步数据流...
- linux上读取不到库文件,linux中make找不到库文件-lmpi的问题
- VB与VB.NET的区别
- 论文翻译——中国武汉市2019年新型冠状病毒感染患者的临床特征
- 数据结构约瑟夫环实验报告
- matlab乖离率计算,乖离率指标详细说明计算
- 小区门禁卡可以复制到手机上吗_如何用手机复制小区门禁卡,一招就可以解决!...