我已经为我的Paint应用程序创建了一个“撤消”功能,但是由于某种原因,它实际上并未删除该路径,它只是将其置于所有其他路径的“下方”。 如果我绘制路径并按撤消,则所有其他路径都将其覆盖。如果没有覆盖它的路径,则删除的路径仍将存在。我该如何解决?
我尝试在撤消函数中使用onDraw()方法,但没有帮助,我尝试执行canvas.drawColor()并绘制背景,但这似乎也没有效果。 我考虑过将所有路径移到另一个arrayList中,从那里绘制然后放回去,但是我认为这样效率不高,而且可能也不起作用。
public void UndoPath() {
if (fingerPaths.size() > 0) {
undoPaths.add(fingerPaths.remove(fingerPaths.size() - 1));
mCanvas.drawColor(currentBGColor);
invalidate();
} else {
Toast.makeText(getContext(), "Nothing to undo!", Toast.LENGTH_SHORT).show();
}
}
public void RedoPath() {
if (undoPaths.size() > 0) {
fingerPaths.add(undoPaths.remove(undoPaths.size() - 1));
invalidate();
} else {
Toast.makeText(getContext(), "Nothing to redo!", Toast.LENGTH_SHORT).show();
}
}
public void clear() {
currentBGColor = defaultBGColor;
fingerPaths.clear();
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
mCanvas.drawColor(currentBGColor);
for (FingerPath fp : fingerPaths) {
mPaint.setColor(fp.color);
mPaint.setStrokeWidth(fp.strokeWidth);
mPaint.setMaskFilter(null);
mCanvas.drawPath(fp.path, mPaint);
}
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.restore();
}
public void Draw(FingerPath fp) {
mPaint.setColor(fp.color);
mPaint.setStrokeWidth(fp.strokeWidth);
mCanvas.drawPath(fp.path, mPaint);
}
private void touchStart(float x, float y) {
mPath = new Path();
FingerPath fp = new FingerPath(currentColor, strokeWidth, mPath);
fingerPaths.add(fp);
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touchMove(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= touchTolerance || dy >= touchTolerance) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touchUp() {
mPath.lineTo(mX, mY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStart(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp();
invalidate();
break;
}
return true;
}
它应该立即删除路径,但是只有在弹出对话框后重新放置视图的焦点之后绘制时,它才会这样做。 另外,由于某种原因,Undo()确实可以立即工作。