Android touch input: Coordinates?
I'm new to Android and have a question rerading touch input (using OnTouchUp/Down/Move). What coordinate system does Android use? I get values ranging from 0,0 (top left) to 320,530 (bottom right) corner. The emulator I chose has a resoluti开发者_JAVA技巧on of 480x800... so that does not make any sense...
Can you give me a hint? Thanks!
This happens because you haven't specified that your application can handle screens with different densities. That's why Android automatically scales everything, but the application works with an imaginary screen, not with the real one.
To make things look as they are you have to either specify android:anyDensity="true"
in AndroidManifest.xml, or set minSdkVersion
or targetSdkVersion
to "4" or higher. The latter will automatically cause Android to assume that android:anyDensity
is set to "true".
You can read more about screen-related issues in the article Supporting Multiple Screens.
To quote from the linked page:
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
精彩评论