如何在画布上停止或暂停绘图

时间:2017-04-04 11:20:47

标签: java android canvas android-studio-2.3

我正在制作脸部编辑应用。我想用手绘画在脸上。我希望能够在画布上停止和暂停绘图。这是我的代码:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.drawBitmap(testbmp, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, mPaint);
    canvas.drawPath(circlePath, circlePaint);
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

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 = 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);
        mX = x;
        mY = y;

        circlePath.reset();
        circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
    }
}

private void touch_up() {
    mPath.lineTo(mX, mY);
    circlePath.reset();
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    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);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
    }
    return true;
}

public void onClickEraser() {
    boolean isEraserOn = true;
    if (isEraserOn)
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    else
        mPaint.setXfermode(null);
}

1 个答案:

答案 0 :(得分:1)

您可以停止onDraw执行。将执行其中的所有方法。 您的应用程序或视图可以实现一些管理绘图队列的机制,onDraw方法将从开始绘制到您决定的位置,但这不是该方法的自然工作方式。