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

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

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

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;

}

我在这里缺少什么?

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值