【发布时间】:2016-07-05 13:07:25
【问题描述】:
我已经完成了通过手指在画布上画线的代码并且也实现了 “撤消”类型的功能。撤消对于不相互交叉的线非常有效,但是当线相互交叉并且我撤消上一行时,它也会在“交叉”点影响另一条线,请查看图片
我用这个代码作图
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 的其余代码相同。所以基本上
我只是用
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
& 结果与图中红色圆圈标记的结果相同。
那么,即使它们具有相同的 xy 坐标,我如何才能只擦除特定线的一部分,我可以在绘制后将绘制的线转换为 ImageView / Bitmap 以便我可以删除 ImageView它自己和它不影响另一条线?
或者有没有更好的方法来实现这一点?
【问题讨论】: