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

23 comments:

  1. I used to have the same confusion about SP/DIP,thanks for your clarifications it is quite helpful.

    ReplyDelete
  2. High performing businesses know that they can no longer ignore the importance of their web and digital initiatives as marketing and communication platforms to connect with their customers and target markets. The challenge for these businesses when looking for a service provider for web design, development and online marketing services, is how to isolate the 'right' service provider from the thousands in the market. An eight year old can build a website these days (yes, I've seen it), so what do businesses serious about succeeding online need to look for? iOS developer

    ReplyDelete
  3. I am glad this it turned out so well and I hope it will continue in the future because it is so interesting and meaningful to the community. app development company in south Africa

    ReplyDelete
  4. I am glad this it turned out so well and I hope it will continue in the future because it is so interesting and meaningful to the community. cheap unlimited reseller hosting

    ReplyDelete
  5. I am glad this it turned out so well and I hope it will continue in the future because it is so interesting and meaningful to the community. kiss918 apk

    ReplyDelete
  6. I am glad this it turned out so well and I hope it will continue in the future because it is so interesting and meaningful to the community. Islam beliefs and practices

    ReplyDelete
  7. The next time I learn a weblog, I hope that it doesnt disappoint me as much as this one. I mean, I do know it was my option to learn, however I really thought youd have one thing interesting to say. All I hear is a bunch of whining about something that you may repair when you werent too busy looking for attention. cheap unlimited reseller hosting

    ReplyDelete
  8. The next time I learn a weblog, I hope that it doesnt disappoint me as much as this one. I mean, I do know it was my option to learn, however I really thought youd have one thing interesting to say. All I hear is a bunch of whining about something that you may repair when you werent too busy looking for attention. family guest house

    ReplyDelete
  9. Recommeneded websites… [...]Here are some of the sites we recommend for our visitors[...]…… hoverboard cheapest price

    ReplyDelete
  10. Recommeneded websites… [...]Here are some of the sites we recommend for our visitors[...]…… voodoo spells

    ReplyDelete
  11. some genuinely choice content on this internet site , saved to my bookmarks . Relax Music

    ReplyDelete
  12. Remarkably! It is as if you read my mind! A person appear to know therefore considerably relating to this, just like you authored the book inside it or something. I feel that you can do with a few images to drive the content residence a bit, on top of that, this really is great weblog. The outstanding study. I will definitely review again. meditation music

    ReplyDelete
  13. Vielen dank für den Beitrag. daraus kann man sich einen guten Eindruck verschaffen Grüsse Heike togel88

    ReplyDelete
  14. I got too much interesting stuff on your blog. I guess I am not the only one having all the enjoyment here! Keep up the good work. 우리카지노

    ReplyDelete
  15. The video card (sometimes referred to as the GPU) is another vital component in any gaming rig, as it handles nearly all of the graphics for your video game titles. A person of the issues with video cards – in particular the center and minimal end ones – is that they tend to come to be obsolete faster than the other components of a gaming pc. Commonly, acquiring a higher conclusion video card when you are upgrading or building your gaming rig is vital as it presents you breathing area in advance of it is time to upgrade once more. Fertility Clinic Orange County

    ReplyDelete
  16. This is such a great resource that you’re offering and you provide out at no cost. I appreciate seeing sites that realize the worth of offering a perfect useful resource totally free. I genuinely loved reading your submit. Mushrooms spores for sale

    ReplyDelete
  17. Rapidly this website may irrefutably end up being well-known amongst just about all running a blog individuals, because of its persistent content articles or even evaluations. ผลบอลสดๆ

    ReplyDelete
  18. I got too much interesting stuff on your blog. I guess I am not the only one having all the enjoyment here! Keep up the good work. Crypto

    ReplyDelete
  19. Sweet site, super pattern , real clean and utilize genial . สล็อตkub

    ReplyDelete
  20. I think this is one of the most important information for me. And i am glad reading your article. But should remark on few general things, The web site style is perfect, the articles is really great : D. Good job, cheers https://socialmediaandemploymentlaw.com/เว็บพนันแทงบอล

    ReplyDelete
  21. Universial Studios offers one of the best studio in Hollywood, they also make great films* https://socialmediaandemploymentlaw.com/เว็บพนันแทงบอล

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete