Question

I am writing an Android app that requires very precise measurements of movement. Ideally, I would like it if the user has their finger on the screen and moves one pixel, I can track it. Currently, I am overriding onTouch to track where the user is. The problem is, when the finger moves fast across the screen, onTouch misses as much as 15px in a movement. Is there a more precise way to do this that onTouch?

This is an example of what I am currently doing:

@Override
public boolean onTouch(View view, MotionEvent event){
    if(event.getAction() == MotionEvent.ACTION_MOVE){
         Log.d(TAG, event.getX() + ", " + event.getY();
    }
}

Thanks.

Was it helpful?

Solution

We had the same issue, and were unable to get further precision. I am not sure the framework will offer pixel-by-pixel movement. However, when a user is making precise movements at slower speeds, this tends to be more accurate. So, if the user has their finger down and literally moves a single pixel, you should get that event. However, if they quickly swipe across 800 pixels, do not expect 800 events.

OTHER TIPS

Here's a bit more precise than just getX & getY: historical touch events. I'm sure this works from SDK8 up, but haven't tested it on lower versions.

    @Override public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        mCurDown = action == MotionEvent.ACTION_DOWN
                || action == MotionEvent.ACTION_MOVE;
        int N = event.getHistorySize();
        for (int i=0; i<N; i++) {
            drawPoint(event.getHistoricalX(i), event.getHistoricalY(i),
                    event.getHistoricalPressure(i),
                    event.getHistoricalSize(i));
        }
        drawPoint(event.getX(), event.getY(), event.getPressure(),
                event.getSize());
        return true;
    }

    private void drawPoint(float x, float y, float pressure, float size) {
        mCurX = (int)x;
        mCurY = (int)y;
        mCurPressure = pressure;
        mCurSize = size;
        mCurWidth = (int)(mCurSize*(getWidth()/3));
        if (mCurWidth < 1) mCurWidth = 1;
        if (mCurDown && mBitmap != null) {
            int pressureLevel = (int)(mCurPressure*255);
            mPaint.setARGB(pressureLevel, 255, 255, 255);
            mCanvas.drawCircle(mCurX, mCurY, mCurWidth, mPaint);
            mRect.set(mCurX-mCurWidth-2, mCurY-mCurWidth-2,
                    mCurX+mCurWidth+2, mCurY+mCurWidth+2);
            invalidate(mRect);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top