我可以画它的路径,我用手指移动用透明线删除,或者根本不画画?
这是我实例化我的橡皮擦的方式:
OnClickListener eraseListener = new OnClickListener() {
@Override
public void onClick(View v) {
mPaint.setColor(0x00000000);
mPaint.setXfermode(clear);
mPaint.setAlpha(0x00);
myView.setPaint(mPaint);
LogService.log("PaintActivity", "------in eraseListener");
}
};
这将从包含画布的View中设置绘画.
在这里,我有以下动议事件:
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveto(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx, dy;
dx = Math.abs(x - mX);
dy = Math.abs(y - mY);
if ((dx >= TOUCH_TOLERANCE) || (dy >= TOUCH_TOLERANCE)) {
undoPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineto(mX, mY);
mPath.moveto(mX, mY);
canvas.drawPath(mPath, paint);
mPath.reset();
}
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
现在,如果我想要擦除,就像我说的那样,当我移动我的手指时,它会在该路径上画一条黑线.当我把手指拉下来时,在touch_up上,它会擦除它所绘制的黑线背面的内容.如果我评论invalidate();从touch_move函数行,然后它不会绘制那条黑线,而且,它只会在touch_up上擦除.我不能让它实时擦除吗?
解决方法:
我这样解决了:
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mPath.lineto(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
mPath.moveto(mX, mY);
mX = x;
mY = y;
}
}
在这里,我在路径中添加了一条线,绘制了路径,重置了路径并在touch_move方法中使用了moveto.
在touch_up上我只使用mPath.reset().
private void touch_up() {
// kill this so we don't double draw
mPath.reset();
}
这使我的橡皮擦透明 – 擦除时没有黑线画.