我已经完成了通过手指在画布上绘制线条的代码。也实现了 “撤消”类型的功能。 Undo对于不相互交叉的线条非常精细,但是当线条相互交叉时我撤消上一行,它影响另一条线以及“交叉”点,请看看图片
用于绘图我使用了这段代码
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(16);
mPaint.setXfermode(null);
//In MotionEvent.ACTION_DOWN:
mPath.reset();
mPath.moveTo(x, y);
// In MotionEvent.ACTION_MOVE:
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
circlePath.reset();
circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
// In MotionEvent.ACTION_UP:
mPath.lineTo(mX, mY);
circlePath.reset();
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
Now from ACTION_DOWN to ACTION_UP i keep track of all the x,y coordinates to use them for undo feature & here's how i Undo
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.TRANSPARENT);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(20);
// This helps to have undo kind of effect
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
ACTION_UP, ACTION_DOWN & ACTION_MOVE
的休息代码是相同的。所以基本上
我只需用相同的x-y坐标绘制另一条线
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
&安培;结果与用红色圆圈标记的相同。
那么我怎么只能删除特定行的部分,即使它们具有相同的xy坐标,我可以在绘制之后将绘制的行转换为ImageView
/ Bitmap
,这样我就可以删除ImageView
它自我和{它不影响另一条线?
或者有更好的方法来实现这一目标吗?
答案 0 :(得分:0)
您无法撤消Canvas
上的绘图。来自documentation:
通过画布,您的绘图实际上是在底层上执行的 位图,放在窗口中。
解决问题的一种方法可能是使用备份Bitmap
在绘制之前保存当前Bitmap
的状态,例如使用Memento pattern。
看看Fast undo/redo for bitmap editor when memory is limited?和Fast undo facility for bitmap editor application(最后一个目标iPhone,但基本想法是相同的)
另一种减少内存消耗的方法是保存您想要在画布上绘制的对象的状态,并根据需要绘制它们。