java画图撤销,使用Canvas在Android绘图应用程序中撤消和重做功能

喻嘉泽
2023-12-01

我正在开发一个我创建绘图镜像的项目。我的主要逻辑工作正常。导致问题的唯一原因是重做和撤消功能。

我搜索了很多。实施可能的方法,但无法取得成功。以下是我的绘图课程。

DrawingView.java

private ArrayList paths = new ArrayList();

private ArrayList undonePaths = new ArrayList();

public DrawingView(Context context, AttributeSet attrs){

super(context, attrs);

this.context=context;

setupDrawing();

}

//setup drawing

private void setupDrawing(){

//prepare for drawing and setup paint stroke properties

brushSize = getResources().getInteger(R.integer.small_size);

lastBrushSize = brushSize;

drawPath = new Path();

drawPath1 = new Path();

drawPaint = new Paint();

drawPaint.setColor(paintColor);

drawPaint.setAntiAlias(true);

drawPaint.setStrokeWidth(brushSize);

drawPaint.setStyle(Paint.Style.STROKE);

drawPaint.setStrokeJoin(Paint.Join.ROUND);

drawPaint.setStrokeCap(Paint.Cap.ROUND);

canvasPaint = new Paint(Paint.DITHER_FLAG);

}

//size assigned to view

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

width=w;

height=h;

Log.d("width,height", w + " , " + h);

drawCanvas = new Canvas(canvasBitmap);

}

//draw the view - will be called after touch event

@Override

protected void onDraw(Canvas canvas) {

for (Path p : paths){canvas.drawPath(p, drawPaint);}

canvas.drawPath(drawPath, drawPaint);

Log.i("OnDRAWING", "REACH ON DRAW");

/*canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);

canvas.drawPath(drawPath, drawPaint);

canvas.drawPath(drawPath1, drawPaint);*/

}

//register user touches as drawing action

@Override

public boolean onTouchEvent(MotionEvent event) {

float touchX = event.getX();

float touchY = event.getY();

//respond to down, move and up events

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

drawPath.reset();

undonePaths.clear();

drawPath.moveTo(touchX, touchY);

undonePaths.clear();

break;

case MotionEvent.ACTION_MOVE:

drawPath.lineTo(touchX, touchY);

break;

case MotionEvent.ACTION_UP:

drawPath.lineTo(touchX, touchY);

drawCanvas.drawPath(drawPath, drawPaint);// commit the path to our offscreen

paths.add(drawPath);

drawCanvas.drawPath(drawPath, drawPaint);

drawPath.reset();

drawCanvas.drawPath(drawPath1, drawPaint);

drawPath1.reset();

break;

default:

return false;

}

//redraw

invalidate();

return true;

}

我在这里缺少什么?

任何建议/想法/例子是在我的项目中实现这种功能的最佳方式吗?

 类似资料: