Android FingerPaint sample does not draw dot?

前端 未结 4 1649
梦如初夏
梦如初夏 2020-12-06 13:50

Fingerpaint example in sample android api demo does not draw dot/point by touching finger on screen. In code they have used Path to draw line, is there any way to draw circl

相关标签:
4条回答
  • 2020-12-06 13:54

    A very late reply, but it would be easier to use mCanvas.drawPoint(x, y, mPaint); in touch_start.

    0 讨论(0)
  • 2020-12-06 14:01

    If you'd rather continue to use your path, store your ACTION_DOWN coordinates and compare them in ACTION_UP. If they haven't moved, add a tiny circle to your path.

    path.addCircle(event.getX(), event.getY(), paint.getStrokeWidth()/4f, Path.Direction.CW);
    

    The advantage of this approach is that it is simple and the circle does not look at all out of place.

    0 讨论(0)
  • 2020-12-06 14:15

    is there any way to draw circle or point using path?

    Instead of trying to do that use the method drawPoint(float x, float y, Paint paint) in the class Canvas.

    To use it in the API demo you will need to change 3 things:

    1. Have a private boolean mDrawPoint; in the MyView class to differentiate between a tap and a slide.
    2. Set mDrawPoint to true in touch_start() and to false in touch_move() if the path is changed (that is in the if statement).
    3. In touch_up() check the value of mDrawPoint. If it is false do what the function did before and if it is true then draw the point on the canvas: mCanvas.drawPoint(mX, mY, mPaint);

    New version of touch_up():

    private void touch_up() {
        if(mDrawPoint == true) {
            mCanvas.drawPoint(mX, mY, mPaint);          
        } else {
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath.reset();
        }
    }
    

    when i move up my finger after drawing a line it automatically draws a point next to it, where the line ended. And when i starts a new line/curve the previously drawn line would remove /clear from the canvas leaving only dots behind that were drawn.

    You don't need any more modification than what's in my answer. You probably forgot to implement a part of it.

    I had the same problem when trying it up and solved it before posting my answer. It was caused because you draw on two different canvas, on given to the onDraw method, which gets lost when your finger gets up and the other is mCanvas, which is where the line is saved in touch_up. This is why touch_up has an if:

    If we didn't move the finger (just tapped) then we draw the point to the mCanvas so that it is still there on the next onDraw (onDraw draws the mCanvas to the canvas it receives as argument so that older lines and points painted on mCanvas are still visible).

    If we moved the finger then we save the path that was drawn to the canvas passed to onDraw to the mCanvas so that it is still present after getting the finger up.

    The problem you have comes from the else part of the touch_up function never getting executed so that on touch_up a point gets drawn regardless of whether it should and the path never gets committed to mCanvas and thus disappear the next time onDraw is called.

    This most likely stem from you setting mDrawPoint to true in touch_start() as I said in point 2. but forgetting to set mDrawPoint up to false in touch_move as I also said in point 2.

    Here is what my touch_move looks like:

    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;
            mDrawPoint = false;
            }
        }
    
    0 讨论(0)
  • 2020-12-06 14:15

    The simple solution that works for me is just to add the following code to touch_start():

    mPath.quadTo(x, y, x + 0.1f, y);
    
    0 讨论(0)
提交回复
热议问题