簡體   English   中英

從最后一行的終點開始新的一行

[英]Start new line from last line's end point

我在一個 Android 應用程序中創建了 canvas,您可以在其中按照手指畫線。 在那一點上一切正常,但我想從最后一行的端點坐標開始新的一行。

我以為我可以將最后一行結束坐標放在新行的操作事件中,但是這樣第一行就沒有坐標,或者它不知道從哪里開始

目前我的代碼就是這樣,這只是行,沒有連接。 這甚至有可能嗎?

    protected float mStartX;
    protected float mStartY;

    protected float mx;
    protected float my;

    private void drawLine(android.graphics.Canvas canvas) {

        float dx = Math.abs(mx - mStartX);
        float dy = Math.abs(my - mStartY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            canvas.drawLine(mStartX, mStartY, mx, my, mPaint);
        }
    }

    private void lineDrawEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isDrawing = true;
                mStartX = mx;
                mStartY = my;
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                isDrawing = false;
                mCanvas.drawLine(mStartX, mStartY, mx, my, mPaintFinal);
                invalidate();
                break;
        }
    }

是的,可以只存儲最后一行的終點以用作下一行的起點,就像您自己說的那樣。

如果你已經有一個起點,你需要做的是改變ACTION_DOWN的行為

就像是:-

protected float mStartX;
protected float mStartY;

protected float mx;
protected float my;

private void drawLine(android.graphics.Canvas canvas) {

    float dx = Math.abs(mx - mStartX);
    float dy = Math.abs(my - mStartY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        canvas.drawLine(mStartX, mStartY, mx, my, mPaint);
    }
}

private void lineDrawEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            isDrawing = true;
            // If this is the first line then the start values will be
            // uninitialised, so only use them for the start of the first line
            // Otherwise they will be set by the previous UP event
            if (mStartX == null && mStartY == null) {
              mStartX = mx;
              mStartY = my;
            }
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            isDrawing = false;
            mCanvas.drawLine(mStartX, mStartY, mx, my, mPaintFinal);
            // Set the Start point of the next line to end of current
            mStartX = mx;
            mStartY = my;
            invalidate();
            break;
    }
}

您可能還希望將每個點添加到列表對 Object 以及,以便您可以在之后對這些線進行一些操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM