Mindril
Mindril

Reputation: 679

Why are touch screen coordinates given in floats?

I'm using Android Studio and I notice that getting the coordinates of a touch returns the float data type. I've always seen screen coordinates represented as ints which makes sense when you're looking at pixels. Does android divide them somehow or are we using some standard group of pixels as the unit? Maybe to represent the width of the average finger?

Upvotes: 3

Views: 340

Answers (2)

kevin
kevin

Reputation: 54

I have tested this, even 1 pixel can be divided in android. U can create a customized view, draw a rect with float coordinates, and compare it with another rect just use int type coordinates, it will be different. It will tell difference less than 1 pixel.

Upvotes: 0

Thomas Kläger
Thomas Kläger

Reputation: 21510

I think this is to allow MotionEvent to support joysticks too.

From the documentation of MotionEvent / DeviceTypes

On joystick devices with source class SOURCE_CLASS_JOYSTICK, the pointer coordinates specify the absolute position of the joystick axes. The joystick axis values are normalized to a range of -1.0 to 1.0 where 0.0 corresponds to the center position. More information about the set of available axes and the range of motion can be obtained using getMotionRange(int). Some common joystick axes are AXIS_X, AXIS_Y, AXIS_HAT_X, AXIS_HAT_Y, AXIS_Z and AXIS_RZ.

If getX() and getY() would return ints that would mean that for joystick devices the return values would be limited to -1, 0 and 1.

Upvotes: 1

Related Questions