Motionevent.getX and getY

Andi Jay picture Andi Jay · Sep 4, 2010 · Viewed 7.2k times · Source

I need help understanding the following.

Say I have a display that is 854x480 pixels. Why is it that the MotionEvent.getX and getY methods return floats? As far as I can tell, the pixels on the display are discrete integers, there is no such thing as a half a pixel on the display.

Answer

Kevin Coppock picture Kevin Coppock · Sep 4, 2010

From the developer page:

"Returns the X coordinate of this event for the given pointer index (use getPointerId(int) to find the pointer identifier for this index). Whole numbers are pixels; the value may have a fraction for input devices that are sub-pixel precise."

http://developer.android.com/reference/android/view/MotionEvent.html#getX()

So for some devices the touch screen may be more precise than just pixel resolution. I would imagine this would be the case the majority of the time for low density devices such as the Droid Eris.

To picture it easier, think of the touchscreen as being completely independent of the screen; like how you can have a drawing tablet of one size that works independently of screen resolution. Say if your resolution is 1600x1200, moving your pen say .02 inches might be the equivalent of 4 or 5 pixels of movement, whereas on an 800x600 screen it would be only 2 or 3. (completely made up numbers, but the point is valid)