How ratio of dp-to-pixel change with the screen density?

2397 views
Skip to first unread message

Xiongzh

unread,
Feb 28, 2009, 11:06:39 PM2/28/09
to Android Developers
I'm being confused by the 'screen density' concept in Android.
I knwe that dpi (dot per inch) came from the printing device. 160 dpi
means 160 dots in every inch on the paper.
However, when dpi is used for the screen density, for example, what's
mentioned '160 dpi screen' in Android document, what's the meaning?
160 pixels on every inch of the display?

As described in http://code.google.com/intl/zh-CN/android/reference/available-resources.html#dimension,
1 dip (density-independent pixel) means 1 pixel on a 160 dpi screen.
Why does the ratio of dp-to-pixel not necessarily change with the
screen density in direct proportion?

When the dpi is changed from 160 to 80, how many pixels will 1dp will
take? 0.5?

I can hardly understand why dip/dp is introduced into Android.

Thank you in advance for explaining that to me.

Romain Guy

unread,
Feb 28, 2009, 11:16:43 PM2/28/09
to android-d...@googlegroups.com
Hi,

The reason for dip to exist is simple enough. Take for instance the
T-Mobile G1. It has a pixel resolution of 320x480 pixels. Now image
another device, with the same physical screen size, but more pixels,
for instance 640x480. This device would have a higher pixel density
than 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.
--
Romain Guy
Android framework engineer
roma...@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them

Xiongzh

unread,
Mar 1, 2009, 11:16:09 PM3/1/09
to Android Developers
Thank you very much for your clear explaination.

Now I agree with you that we had better use dip/sp as the dimention
unit to design the UI.
However when we shall use px/mm/pt?
> > As described inhttp://code.google.com/intl/zh-CN/android/reference/available-resourc...,
> > 1 dip (density-independent pixel) means 1 pixel on a 160 dpi screen.
> > Why does the ratio of dp-to-pixel not necessarily change with the
> > screen density in direct proportion?
>
> > When the dpi is changed from 160 to 80, how many pixels will 1dp will
> > take? 0.5?
>
> > I can hardly understand why dip/dp is introduced into Android.
>
> > Thank you in advance for explaining that to me.
>
> --
> Romain Guy
> Android framework engineer
> romain...@android.com

e3075

unread,
Mar 10, 2009, 2:27:17 AM3/10/09
to Android Developers
Thanks for making clarifications on "dip" which is quite helpful.

However can someone help to explain "sp"?
In Android doc, it says "but it is also scaled by the user's font
size preference", what is "font size preference" being referred here?
Comparing with "dip", "sp" has a extended meaning than "dip" with
this "font size preference" thing, is that right to define the
relationship between "dip" and "sp"?

I tried to search the web but couldn't get a satisfied answer to it.
Maybe an example will be helpful to clarify.

Thanks a lot in advance!

On Mar 2, 12:16 pm, Xiongzh <zx.zhangxi...@gmail.com> wrote:
> Thank you very much for your clear explaination.
>
> Now I agree with you that we had better usedip/sp as the dimention
> unit to design the UI.
> However when we shall use px/mm/pt?
>
> On 3月1日, 下午12时16分, Romain Guy <romain...@google.com> wrote:
>
>
>
> > Hi,
>
> > The reason fordipto exist is simple enough. Take for instance the
> > T-Mobile G1. It has a pixel resolution of 320x480 pixels. Now image
> > another device, with the same physical screen size, but more pixels,
> > for instance 640x480. This device would have a higher pixel density
> > than 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
> >dipwhen 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.
>
> > On Sat, Feb 28, 2009 at 8:06 PM, Xiongzh <zx.zhangxi...@gmail.com> wrote:
>
> > > I'm being confused by the 'screen density' concept in Android.
> > > I knwe that dpi (dot per inch) came from the printing device. 160 dpi
> > > means 160 dots in every inch on the paper.
> > > However, when dpi is used for the screen density, for example, what's
> > > mentioned '160 dpi screen' in Android document, what's the meaning?
> > > 160 pixels on every inch of the display?
>
> > > As described inhttp://code.google.com/intl/zh-CN/android/reference/available-resourc...,
> > > 1dip(density-independent pixel) means 1 pixel on a 160 dpi screen.
> > > Why does the ratio of dp-to-pixel not necessarily change with the
> > > screen density in direct proportion?
>
> > > When the dpi is changed from 160 to 80, how many pixels will 1dp will
> > > take? 0.5?
>
> > > I can hardly understand whydip/dp is introduced into Android.

Dianne Hackborn

unread,
Mar 10, 2009, 12:59:44 PM3/10/09
to android-d...@googlegroups.com
The px, mm, and pt units are absolute dimensions.  If the device is configured correctly (the current system on the G1 unfortunately has bugs here), then 1 px will be exactly one inch on screen.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support.  All such questions should be posted on public forums, where I and others can see and answer them.

Dianne Hackborn

unread,
Mar 10, 2009, 1:00:50 PM3/10/09
to android-d...@googlegroups.com
On Mon, Mar 9, 2009 at 10:27 PM, e3075 <e30...@gmail.com> wrote:
However can someone help to explain "sp"?
 In Android doc, it says "but it is also scaled by the user's font
size preference", what is "font size preference" being referred here?

It's a setting that isn't currently exposed in preferences, allowing you to make fonts larger or smaller.  If you build Spare Parts from the 1.0 source tree you can install that and use it to change the font size preference.

--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support.  All such questions should be posted on public forums, where I and others can see and answer them.

Reply all
Reply to author
Forward
0 new messages