下面贴出代码。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class Draw extends View implements View.OnClickListener {
private static final String TAG = "Draw";
private Canvas canvas;//声明画笔
private Paint paint;//声明画布
private Bitmap bitmap;//声明位图
private float[] mStartXs = new float[20];
private float[] mStartYs = new float[20];
private float[] mEndXs = new float[20];
private float[] mEndYs = new float[20];
public Draw(Context context) {
super(context);
// TODO 自动生成的构造函数存根
paint = new Paint(Paint.DITHER_FLAG);//创建一个画笔
bitmap = Bitmap.createBitmap(1280, 800, Bitmap.Config.RGB_565);//设置位图的宽高
canvas = new Canvas(bitmap);
paint.setStyle(Paint.Style.STROKE);//设置非填充
paint.setStrokeWidth(5);//笔宽5像素
paint.setColor(Color.RED);//设置为红笔
paint.setAntiAlias(true);//锯齿不显示
paint.setDither(true);//设置图像抖动处理
paint.setStrokeJoin(Paint.Join.ROUND);//设置图像的结合方式
paint.setStrokeCap(Paint.Cap.ROUND);//设置画笔为圆形样式
}
@Override
protected void onDraw(Canvas canvas) {
// TODO 自动生成的方法存根
super.onDraw(canvas);
canvas.drawBitmap(bitmap, 0, 0, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO 自动生成的方法存根
// 获取事件类型
int action = event.getAction() & MotionEvent.ACTION_MASK;
// 获取index编号
int index = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG,"第1个手指按下");
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
for (int i = 0; i < event.getPointerCount(); i++) {
mStartXs[i] = event.getX(i) ;//获取手指落下的x坐标
mStartYs[i] = event.getY(i);//获取手指落下的y坐标
canvas.drawPoint(mStartXs[i], mStartYs[i], paint);//在画布上画点
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
Log.d(TAG,"第"+(index+1)+"个手指按下");
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
for (int i = 0; i < event.getPointerCount(); i++) {
mStartXs[i] = event.getX(i) ;
mStartYs[i] = event.getY(i);
canvas.drawPoint(mStartXs[i], mStartYs[i], paint);
}
break;
case MotionEvent.ACTION_MOVE:
for (int i = 0; i < event.getPointerCount(); i++) {
int pointerId = event.getPointerId(i);
Log.d(TAG, "pointerIndex="+i+", pointerId= "+ pointerId);
// TODO
mEndXs[pointerId] = event.getX(i);//获取手指移动的x坐标
mEndYs[pointerId] = event.getY(i);//获取手指移动的y坐标
canvas.drawLine(mStartXs[pointerId], mStartYs[pointerId] , mEndXs[pointerId] , mEndYs[pointerId], paint);//在画布上画线
mStartXs[pointerId] = mEndXs[pointerId];
mStartYs[pointerId] = mEndYs[pointerId];
}
break;
case MotionEvent.ACTION_UP:
Log.d(TAG,"MotionEvent.ACTION_UP");
break;
}
invalidate();//使绘画动作生效
return true;
}
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
}
}
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class DrawLineActivity extends AppCompatActivity {
Draw draw;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//新建一个Draw类
draw = new Draw(DrawLineActivity.this);
//将界面设为Draw类
setContentView(draw);
}
}
参考记录:
在Android屏幕上画图
https://blog.csdn.net/u014129816/article/details/39970081
Android 自定义view,画图板练习
https://www.jianshu.com/p/f05751dee57f
Android多点触控之PointerId与PointerIndex
https://www.jianshu.com/p/3eca050b90ed