从画布上删除,最后一个canvas.drawPath()

时间:2013-02-13 14:06:19

标签: android canvas paint erase stroke

这是我制作的代码:

private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    undoPath = new Path();
    undoPath.moveTo(x, y);
    canvas.drawPoint(x, y, paint);
    mX = x;
    mY = y;
    if (bitmaps.size() > 0) {
        for (int i = 0; i < (bitmaps.size()); i++) {
            System.out.println("CustomImage.touch_start(): " + i + "||" + bitmaps.get(i).rect + "ZZ: " + x + "|| " + y);
            if (bitmaps.get(i).rect.contains((int) x, (int) y)) {
                System.out.println("CustomImage.touch_start()2");
                contains = true;
                lastBitmap = i;
            }
        }
    }

 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)) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        undoPath.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);
    undoPath.lineTo(mX, mY);
    undoPath.moveTo(mX, mY);
    canvas.drawPath(mPath, paint);
    mPath.reset();
}
    @Override
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;
}

当我按下撤销按钮时,我这样做:

 public void setPaint2(Paint paint2) {
    canvas.drawPath(undoPath, paint2);
    invalidate();
}

其中paint2是:

    paint2= new Paint();
    paint2.setStrokeWidth(paint.getStrokeWidth()+5);
    paint2.setColor(0x00000000);
    paint2.setXfermode(clear);
    paint2.setAlpha(0x00);

paint是我用来绘制的Paint(),paint2是我用来擦除的颜色(在第一个上涂有alpha = 0(透明)的颜色)。 问题在于,即使它擦除了一个部分,它也不会删除所有内容,我该怎么办?

1 个答案:

答案 0 :(得分:1)

这解决了我的问题:

delPaint = new Paint();
            delPaint.setColor(0x00000000);
            delPaint.setXfermode(clear);
            delPaint.setAlpha(0x00);
            delPaint.setAntiAlias(true);
            delPaint.setDither(true);
            delPaint.setStyle(Paint.Style.STROKE);
            delPaint.setStrokeJoin(Paint.Join.ROUND);
            delPaint.setStrokeCap(Paint.Cap.ROUND);