Android:让用户从一点到另一点画一条线

Android: Let user draw a line from one point to another

我正在创建一个 Android 应用程序,用户可以在其中使用有源笔在平板电脑上绘图和书写。 用户可以在不同的模式(例如笔、橡皮擦、线、圆...)之间进行选择,这为他们提供了不同的工具。
直线和圆圈工具让用户可以使用 length/radius 和用户绘制方向绘制 line/circle。这工作得很好,但每次用户移动笔时,都会绘制另一个 line/circle 并填满屏幕。

图片:

代码:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas c){
    c.drawBitmap(mBitmap, 0, 0, bitmapPaint);
}

private float mX, mY, firstX, firstY;

private void touch_start(float x, float y) { //called from MotionEvent.ACTION_DOWN
    path.moveTo(x, y);
    firstX = x;
    firstY = y;
    mX = x;
    mY = y;
}

private void touch_move(float x, float y) { //called from MotionEvent.ACTION_MOVE
    path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
    switch(mode){
        case "line":
            canvas.drawLine(firstX, firstY, x, y, paint);
            break;
        case "circle":
            canvas.drawCircle(firstX, firstY, (Math.abs(firstX-x) + Math.abs(firstY-y))/1.5f, paint); // divisor 1.5 just a rough value
            break;
        default:
            canvas.drawPath(path, paint);
            break;
    }
    mX = x;
    mY = y;
}

有人知道我该如何解决这个问题吗?
提前致谢。

我自己找到了解决方法。
我创建了一个新的 Canvas animationCanvas,它有自己的 animationBitmap 用于在绘制时显示圆。
最后的圆是在 MotionEvent.ACTION_UP.

处的方法中绘制的

这是新的 touch_move:

private void touch_move(float x, float y) { //called from MotionEvent.ACTION_MOVE
    path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
    switch(mode){
        case "line":
            animationCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); // clear previously drawn line (for animation)
            animationCanvas.drawLine(firstX, firstY, x, y, paint);
            break;
        case "circle":
            animationCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); // clear previously drawn circle (for animation)
            animationCanvas.drawCircle(firstX, firstY, (Math.abs(firstX-x) + Math.abs(firstY-y))/1.5f, paint); // divisor 1.5 just a rough value 
            break;
        default:
            canvas.drawPath(path, paint);
            break;
    }    
    mX = x;
    mY = y;
}

也许不是最好的解决方案,但它确实有效:)