I am working on an application based on Galaxy S at the moment. I know that Galaxy S is 480 px wide and 800 px tall, but how much is that in DP?
Lets say if I want to have two Layout side by side, I'll have them setting to 240 px. But how do I know what value I should use in DP unit?
A dp is equal to one physical pixel on a screen with a density of 160. To calculate dp: dp = (width in pixels *... A dp is equal to one physical pixel on a screen with a density of 160.
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.
To preserve the visible size of your UI on screens with different densities, you must design your UI using density-independent pixels (dp) as your unit of measurement. One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density).
The conversion of dip units to screen pixels is simple: pixels = dips * (density / 160). For example, on 240 dpi screen, 1 dip would equal 1.5 physical pixels. Using dip units to define your application's UI is highly recommended, as a way of ensuring proper display of your UI on different screens.
Found: http://developer.android.com/guide/practices/screens_support.html
[edit]
I just had to used this. Using the DisplayMetrics.density
returns only 0.75, 1 and 1.5. Use DisplayMetrics.densityDpi
instead or change the math to pixels = dips * DisplayMetrics.density
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With