Android – eraser with porterduff.mode.clear always draws a black line, I want to delete it
Can I draw its path, move it with my fingers, delete it with transparent lines, or don't draw at all?
This is how I instantiate my eraser:
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");
}
};
This will set the painting from the view containing the canvas. Here, I have the following motion events:
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;
}
Now, if I want to erase, as I said, when I move my finger, it will draw a black line on the path. When I pull my finger down, it will be in touch_ Up, it will erase the contents on the back of the black line it draws. If I comment on invalidate(); From touch_ The move function line, and then it won't draw the black line, and it will only be in touch_ Up. Can't I erase it in real time?
resolvent:
I solved this:
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;
}
}
Here, I add a line to the path, draw the path, reset the path and click touch_ Moveto. Is used in the move method
In touch_ Up, I only use mpath. Reset()
private void touch_up() {
// kill this so we don't double draw
mPath.reset();
}
This makes my eraser transparent - there are no black lines when erasing