Android自定义类-写字板

server/2024/10/11 13:23:50/

目录

1. 属性文件 res/values/attrs.xml

2. 自定义控件类文件 MyClipbroad.class

3. XML布局文件中的使用

4. Java文件中的使用


该写字板可设置画笔颜色、画笔宽度、画布背景,具有导出图像、清空画布功能,可与OnTouchListener配合达到触摸绘画的效果。

1. 属性文件 res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="MyClipboard"><attr name="paintColor" format="color" /><attr name="paintWidth" format="integer" /></declare-styleable>
</resources>

2. 自定义控件类文件 MyClipbroad.class

java">import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;public class MyClipboard extends RelativeLayout {private int viewHeight,viewWidth;private Path path=new Path();private int paintColor=R.color.black;private int paintWidth=5;public MyClipboard(Context context) {super(context);setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));}@SuppressLint("ResourceAsColor")public MyClipboard(Context context, @Nullable AttributeSet attrs) {super(context, attrs);TypedArray typedArray =context.obtainStyledAttributes(attrs,R.styleable.MyClipboard);paintColor=typedArray.getColor(typedArray.getIndex(R.styleable.MyClipboard_paintColor), ContextCompat.getColor(context,R.color.black));paintWidth=typedArray.getInt(typedArray.getIndex(R.styleable.MyClipboard_paintWidth),5);typedArray.recycle();setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);viewHeight=MeasureSpec.getSize(heightMeasureSpec);viewWidth=MeasureSpec.getSize(heightMeasureSpec);}@SuppressLint("ResourceAsColor")protected void onDraw(Canvas canvas) {super.dispatchDraw(canvas);Paint paint=new Paint(paintColor);paint.setStrokeWidth(paintWidth);paint.setStyle(Paint.Style.STROKE);canvas.drawPath(path,paint);}/*** 绘制直线* @param x1* @param y1* @param x2* @param y2*/public void draw(float x1,float y1,float x2,float y2){path.moveTo(x1,y1);path.lineTo(x2,y2);invalidate();}/*** 清空图像*/public void cleanDraw(){Log.d("OK","clean");path=new Path();invalidate();}/*** 导出当前图像* @return 位图Bitmap*/public Bitmap getBitmap(){Bitmap bitmap=Bitmap.createBitmap(viewWidth,viewHeight, Bitmap.Config.ARGB_8888);Canvas canvas=new Canvas(bitmap);Paint paint=new Paint(paintColor);paint.setStrokeWidth(paintWidth);paint.setStyle(Paint.Style.STROKE);canvas.drawPath(path,paint);return bitmap;}
}

3. XML布局文件中的使用

请注意一定要设置background属性!

<com.example.clipboard.MyClipboardandroid:id="@+id/myc"android:layout_width="match_parent"android:layout_height="match_parent"app:paintColor="@color/teal_200"app:paintWidth="5"android:background="@color/white"/>

4. Java文件中的使用

java">//获取控件MyClipboard myClipboard=findViewById(R.id.myc);//添加监听器myClipboard.setOnTouchListener(new View.OnTouchListener() {private float oldX=0,oldY=0;public boolean onTouch(View view, MotionEvent motionEvent) {int action=motionEvent.getAction();if(action==MotionEvent.ACTION_DOWN){oldX=motionEvent.getX();oldY=motionEvent.getY();return true;}else if (action==MotionEvent.ACTION_MOVE) {float newX=motionEvent.getX();float newY=motionEvent.getY();((MyClipboard)view).draw(oldX,oldY,newX,newY);oldX=newX;oldY=newY;return true;}    else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {float newX=motionEvent.getX();float newY=motionEvent.getY();((MyClipboard)view).draw(oldX,oldY,newX,newY);oldX=0;oldY=0;return true;}return false;}
});//清空图像
myClipboard.cleanDraw();//导出图像
Bitmap bitmap=myClipboard.getBitmap();

tag:画板;手写板


http://www.ppmy.cn/server/6211.html

相关文章

【QT学习】7.事件,把文本显示在页面中(文本可变),鼠标指针切换,鼠标左键右键按下,qt设置背景样式

0.创建项目&#xff0c;事件的创建 1.事件的位置 2.这就是多态&#xff0c;子类重写父类函数&#xff0c;子类调用子类函数&#xff0c;也可以调用父类函数。但同函数名 1.要求&#xff1a;文本显示在页面中&#xff08;文本可变&#xff09; 1.文本显示在页面的核心代码 主要步…

无法连接到MongoDB Atlas 的Cloud Database

打开Mongodb网页: 选择允许任何地址连接 连接成功

hyref 开发者推荐项目

GitHub - limingxinleo/phalcon: phalcon项目结构 GitHub - fan3750060/wpcore: php , swoole, wow, wowcore, World of Warcraft,master> wow_3.3.5a(Can not work normally) ,branch>wow_2.4.3_8606

车轮上的智能:探索机器学习在汽车行业的应用前景

文章目录 引言&#xff1a;一、机器学习在汽车设计中的应用设计优化模拟与测试 二、智能制造与生产三、自动驾驶技术感知与决策数据融合 四、市场与模式的变革五、机器学习对于汽车行业的机遇与挑战挑战机遇 引言&#xff1a; 在当今数字化时代&#xff0c;机器学习作为人工智…

Unity UGUI透明区域点击无效

是这样的&#xff0c;我有一张图&#xff0c;客户给的是1920*1080&#xff0c;但只有中间部分是按钮&#xff0c;是有效像素。为了让空白区域点击无效。需要设置如下 并且加上下面这句 this.GetComponent<Image>().alphaHitTestMinimumThreshold 0.1f;

多语言vue-i18n (vue2,uniapp)

安装vue-i18n npm install vue-i18n8 --save // npm install vue-i18n–save 9版本需要vue3.0 // 在vue2环境下&#xff0c;默认安装 npm install vue-i18n 的版本是 vue-i18n9.2.2&#xff0c; // 报错信息里提示这个版本要求是vue3&#xff0c;所以我们安装适合vue2版本的vu…

Android活动之Intent

Intent Intent是Android程序中各组件之间进行交互的一种重要方式&#xff0c;它不仅可以指明当前组件想要执行的动作&#xff0c;还可以在不同组件之间传递数据。Intent一般可被用于启动活动、启动服务以及发送广播等场景&#xff0c; 显示intent 第一个参数Context要求提供…

从 Android 恢复已删除文件的 3 种简单方法

如何从 Android 恢复已删除的文件&#xff1f;毫不犹豫&#xff0c;有些人可能会认为从 Google 备份恢复 Android 文件太容易了。但是&#xff0c;如果删除的文件未同步到您的帐户或未备份怎么办&#xff1f;您错误的恢复可能会永久删除您想要的数据。因此&#xff0c;我们发布…