Android studio实现画板功能
作者:假快乐 发布时间:2022-08-04 21:30:39
标签:Android,studio,画板
简单概述
在日常生活中,我们经常会突发一些奇思妙想,或是一个画面,或是几个符号。这时候无法使用拍照或者打字功能实现,想拿笔记下又身边找不到笔。于是我琢磨能不能做一个手机端的画板。
效果图
实现过程
项目布局很简单
让我们来看代码:首先声明画笔,画板,和坐标
public class MainActivity extends AppCompatActivity{
Paint paint;
Canvas canvas;
ImageView imageview;
Bitmap bitmap,newbitmap;
TextView tv_stroke;
int startX, startY, endX, endY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_paint_tools);
LinearLayout ll_layout = findViewById(R.id.ll_layout);
RadioGroup rg_color = findViewById(R.id.rg_color);
遍历单选按钮,当单选按钮选中时,获取单选按钮颜色并将画笔颜色设置当前按钮的文本颜色,最后注意要设置画笔宽度,以免在后面点橡皮擦的时候画笔宽度调不回来
for (int i = 0;i<rg_color.getChildCount();i++){
RadioButton rb = (RadioButton) rg_color.getChildAt(i);
rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()){
paint.setColor(buttonView.getTextColors().getDefaultColor());
paint.setStrokeWidth(5);
}
}
});
}
首先创建一张空白图片和一张灰色画布,将图片放在画布上面
注册触摸监听事件,获取鼠标按下时的坐标和鼠标移动后的坐标。在开始和结束之间画一条直线并更新画布图片
imageview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.i("MyPaintToolsActivity","ACTION_DOWN");
startX = (int) (event.getX()/1.4);
startY = (int) (event.getY()/1.4);
break;
case MotionEvent.ACTION_MOVE:
Log.i("MyPaintToolsActivity","ACTION_MOVE");
endX = (int) (event.getX()/1.4);
endY = (int) (event.getY()/1.4);
canvas.drawLine(startX,startY,endX,endY,paint);
startX = (int) (event.getX()/1.4);
startY = (int) (event.getY()/1.4);
imageview.setImageBitmap(bitmap);
break;
case MotionEvent.ACTION_UP:
Log.i("MyPaintToolsActivity","ACTION_UP");
break;
}
imageview.invalidate();
return true;
}
});
清屏的话就一行代码 ,剩下的是重新生成一块画布
Button btn_clear = findViewById(R.id.btn_clear);
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
canvas.drawColor(0,PorterDuff.Mode.CLEAR);
bitmap = Bitmap.createBitmap(888,1200,Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
canvas.drawColor(Color.argb(100,0,0,0));
paint = new Paint();
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
paint.setColor(Color.RED);
canvas.drawBitmap(bitmap,new Matrix(),paint);
imageview.setImageBitmap(bitmap);
}
});
呃,这里会把画布擦掉…也就是擦成白色…
最后看看页面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_layout">
<!-- tools:context=".MyPaintToolsActivity">-->
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<RadioGroup
android:background="#747373"
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="@+id/rg_color"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb_red"
android:layout_width="wrap_content"
android:layout_height="43dp"
android:layout_weight="1"
android:text="红色"
android:textColor="#FF0000"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb_green"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:text="黑色"
android:textColor="#000000"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb_blue"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:text="白色"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#000000"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:text="清除"/>
<Button
android:id="@+id/btn_eraser"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:background="#000000"
android:text="擦除"/>
</LinearLayout>
</LinearLayout>
来源:https://blog.csdn.net/weixin_44470908/article/details/112095504


猜你喜欢
- java 交换两个数据的方法1:利用数组,即先把要交换的数字放在数组中 ,比如在一些数组排序中可能用到public static void
- 本文实例为大家分享了java实现图书馆管理系统的具体代码,供大家参考,具体内容如下思路:所有包都在book_manage包里利用面向对象的多
- 自定义控件(类似按钮等)的使用,自定义一个SurfaceView。 如某一块的动态图(自定义相应),或者类似UC浏览器下面的工具栏。 如下图
- 相信各位同道在写代码的时候,肯定会写一些日志打印,因为这对往后的运维而言,至关重要的。那么我们请求一个restfull接口的时候,哪些信息是
- 一、非配置文件注入1、注入普通字符串直接附在属性名上,在 Bean 初始化时,会赋初始值。@Value("admin")
- 本文实例讲述了Java基于分治算法实现的棋盘覆盖问题。分享给大家供大家参考,具体如下:在一个2^k * 2^k个方格组成的棋盘中,有一个方格
- 本文实例为大家分享了toolabar结合drawlayout使用方法,供大家参考,具体内容如下package alice.bw.com.da
- C#操作注册表全攻略相信每个人对注册表并不陌生,在运行里面输入“regedit”就可以打开注册表编辑器了。这东西对Windows系统来说可是
- 使用POI读写Word doc文件 Apache poi的hwpf模
- 短网址应用已经在全国各大微博上开始流行了起来。例如QQ微博的url.cn,新郎的sinaurl.cn等。我们在QQ微博上发布网址的时候,微博
- 概述1、邮件相关的标准厂商所提供的 JavaMail 服务程序可以有选择地实现某些邮件协议,常见的邮件协议包括:SMTP(Simple Ma
- 1.常用属性Name:名称;BackColor:设置控件背景颜色;Enabled:是否可用;FlayStyle:控件样式;Image:设置控
- 1. 内部类很多时候我们创建类的对象的时候并不需要使用很多次,每次只使用一次,这个时候我们就可以使用内部类了1.1 内部类概述内部类就是在一
- 这篇文章主要介绍了springboot多租户设计过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 大家在使用 Intellij IDEA 的时候会经常遇到各种乱码问题,甚是烦扰。栈长也偶尔会用下IDEA,也有一些解决乱码的经验,我给大家总
- 需求: 使用IO流将指定目录下的若干个音频文件的高潮部分,进行剪切,并重新拼接成一首新的音频文件 思路(以两首歌为例):第一首歌有
- this template depends on the android support library,which is either n
- 一、基本概念(重要)Integer 是 int 的包装类,int 则是 java 的一种基本数据类型;Integer 变量必须实例化后才能使
- Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动。下面是我这几年做开发过程中自己用过的工具类
- 1.情景展示java发送get请求、post请求(form表单、json数据)至另一服务器;可设置HTTP请求头部信息,可以接收服务器返回c