basics of device-independent-pixels
Asked Answered
B

4

7

im throughoutly confused by dips on Android.

I understand from the reference that the base for dp values is 160. So, shouldn't 80dp in width equals a view with a width of 50% of the screen ? On my Nexus One the width in dp is something around 300dp as it seems.

What am i missing here ?

thx in advance

Bounty answered 16/1, 2011 at 18:48 Comment(0)
C
16

"dp" == "Density-independent Pixels" (This is also why it was earlier called "dip", though I prefer to use "dp" these days.)

Think of it like other units -- "in" (inches), "mm" (millimeters), etc. It allows you to provide a size that is scaled based on the density of the screen.

We define mdpi to be the base density, so "10dp" on an mdpi screen will result in exactly 10 pixels. On an hdpi screen it will result in 15 pixels, because hdpi is 1.5*mdpi.

Note that though the constants for various densities are similar to DPI (mdpi is 160, etc), density is not exactly DPI. It is an abstract scaling factor that adjusts for screen dpi, but does not try to exactly reflect it. (You would use "in", "mm", etc for exact sizes but 99.9% that is not what you want so stick with "dp".) This greatly simplifies life for everyone because you don't need to deal with many Android devices having a slightly different amount of space for its UI because they each of slight different screen DPIs. Also, device manufacturers can select the density of their device to achieve a desired UI -- for example the Samsung Tab uses a density that is a fair amount larger than the actual DPI, resulting in an overall larger UI.

Campanology answered 16/1, 2011 at 22:1 Comment(1)
"This greatly simplifies life for everyone" - except it doesn't. It makes life harder for everyone - because dips don't work (they are so VERY different on many pieces of hardware now that they are useless). Google still says to use dip for all UI, but that is impossible since it causes alighments to be completely wrong on 30% of all handsets (I came here looking for a solution, disappointed to find none).Dubois
W
5

160 dots per inch. So 80dp would be 1/2 an inch, roughly.

Waldemar answered 16/1, 2011 at 20:53 Comment(2)
OP asks about dip, not dpi.Magna
They're related. He mentioned 160 being the base value -- that value is the density in DPI.Waldemar
M
2

I don't understand your question completely but I suggest you take a look at this, if you haven't already.

http://developer.android.com/guide/practices/screens_support.html

pixels = dps * (density / 160)

Martins answered 16/1, 2011 at 18:57 Comment(0)
M
0

Density independent pixels (short: dp) are a virtual pixel unit that will be determined at the runtime of your application.

Formala: 1 dp = 1 Pixel on 160 dpi screen. So 160 dpi is the baseline density for the system.

The conversion of dp units to screen pixels are quite simple. Actual device pixels (px) = dp (1) * (dpi (of the device) / 160(baseline) ) For the sake of simplicity: px = dp * (dpi / 160)

Example: If a 240 dpi device starts your app, then 1 dp equals to 1,5 actual device pixels.

Conclusion: Dp automatically handles any scaling to bigger or smaller devices. The times where you hardcode the pixels are over. DP ensures the proper scaling on different screen densities.

Member answered 15/4, 2017 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.