带有PorterDuff.Mode.CLEAR的橡皮擦总是绘制一条黑线,我要删除

ros*_*lin 4 android canvas path paint erase

我可以画它的路径,我用手指移动用透明线删除,或者根本不画画?

这是我实例化我的橡皮擦的方式:

 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");

        }
    };
Run Code Online (Sandbox Code Playgroud)

这将从包含画布的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;
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我想要擦除,就像我说的那样,当我移动我的手指时,它会在该路径上画一条黑线.当我把手指拉下来时,在touch_up上,它会擦除​​它所绘制的黑线背面的内容.如果我评论invalidate(); 从touch_move函数行,然后它不会绘制那条黑线,而且,它只会在touch_up上擦除.我不能让它实时擦除吗?

小智 11

我这样解决了:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我在路径中添加了一条线,绘制了路径,重置路径并moveTotouch_move方法中使用.

touch_up我只用mPath.reset().

private void touch_up() {

    // kill this so we don't double draw
     mPath.reset();

}
Run Code Online (Sandbox Code Playgroud)

这使我的橡皮擦透明 - 擦除时没有黑线画.


sit*_*al_ 7

只需关闭HWA即可.

或者您可以在构造函数中添加此行

setLayerType(View.LAYER_TYPE_SOFTWARE, mPaint);
Run Code Online (Sandbox Code Playgroud)