Saturday, February 28, 2009

Dimension Values of Android API

The document of Android API 1.0 rc2
You can create common dimensions to use for various screen elements by defining dimension values in XML. A dimension resource is a number followed by a unit of measurement. For example: 10px, 2in, 5sp. Here are the units of measurement supported by Android:
px
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
mm
Millimeters - based on the physical size of the screen.
pt
Points - 1/72 of an inch based on the physical size of the screen.
dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.


My understanding
px, in, mm and pt are all absolute units.
px is based on the resolution of the screen.
in, mm and pt are based on the physical size of the screen.
dip/sp can accommodate size with the resolution to make it appear exactly the same size.

Here's the comment got from reply in the android developer group:
[Romain Guy]
If you specify, in your application, a button with a width of 100
pixels, it will look at lot smaller on the 640x480 device than on the
320x480 device. Now, if you specify the width of the button to be 100
dip, the button will appear to have exactly the same size on the two
devices.

You can easily see this happen when you compare the T-Mobile G1 with
the Android emulator. Computer monitors usually have low/medium pixel
densities. For instance, the monitor I'm using to write this email has
a density of about 100 pixels per inch, whereas the G1 has about 180
pixels per inch. This means that when I compare my application on the
G1 with my application on the Android emulator on my computer, the
version on my computer appears a lot bigger to me.

It is very important that you use resolution independent units like
dip when you create your UI. This well help make your application run
on future Android devices that may or may not have the same pixel
density as the G1.
If you specify, in your application, a button with a width of 100
pixels, it will look at lot smaller on the 640x480 device than on the
320x480 device. Now, if you specify the width of the button to be 100
dip, the button will appear to have exactly the same size on the two
devices.

You can easily see this happen when you compare the T-Mobile G1 with
the Android emulator. Computer monitors usually have low/medium pixel
densities. For instance, the monitor I'm using to write this email has
a density of about 100 pixels per inch, whereas the G1 has about 180
pixels per inch. This means that when I compare my application on the
G1 with my application on the Android emulator on my computer, the
version on my computer appears a lot bigger to me.

It is very important that you use resolution independent units like
dip when you create your UI. This well help make your application run
on future Android devices that may or may not have the same pixel
density as the G1.
The conclusion is that you should use dip/sp as many as possible unless you have solid reason to use the other dementions instead.
In development you can also telnet to the simulator (telnet localhost 5554) to use the 'window scale' command to change the screen density to verify if your application can accomadate with various screens.

References:
1. http://vod.sjtu.edu.cn/help/Article_Show.asp?ArticleID=308&ArticlePage=11.
2. http://baike.baidu.com/view/49853.html
3. http://fbljava.blog.163.com/blog/static/26521174200871383754608/#
4. http://groups.google.com/group/android-developers/browse_thread/thread/978813b2998ef439/f63e4e51bdb2e2d5#f63e4e51bdb2e2d5