2

I am writing simple android application for drawing on screen based on FingerPaint API demo example. In the demo drawing begins only after finger has traveled certain distance on screen defined by TOUCH_TOLERANCE. I'd like to draw a dot even if user doesn't move the finger. Is it possible?

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFAAAAAA);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }

    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);
        //mPath.lineTo(x + 1, y + 1); //quick fix
        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;
        }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        // 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;
    }
Oto Zars
  • 623
  • 10
  • 17
  • Your question has been answered here: http://stackoverflow.com/questions/17251416/android-fingerpaint-sample-does-not-draw-dot – Viktor Brešan Jul 26 '13 at 13:39

2 Answers2

3

Yes, it is possible. Just add 1 to the x or y Like this

//Create a dot
path.setLastPoint(x, y);
x++;
path.lineTo(x, y);
NaviRamyle
  • 3,967
  • 1
  • 31
  • 49
2

Adding a small circle looks better, but it takes more time while drawing if a lot of circles are added to the path:

path.addCircle(x, y, 1, Path.Direction.CCW);

So, adding a small rectangle around the point will do the trick:

path.addRect(x - 0.5f, y - 0.5f, x + 0.5f, y + 0.5f, Path.Direction.CCW);
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59