-2

As per android doc says "The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels."

I'm bit confused on the example. As per the formula, px=(240/160)dp => px = 1.5dp how come it becomes "on a 240 dpi screen, 1 dp equals 1.5 physical pixels."? It should rather say 1 px equals 1.5 dp. Please make me clear.

Pratik
  • 30,639
  • 18
  • 84
  • 159
Neomat Smith
  • 43
  • 1
  • 7
  • 1
    [Here is][1] the answer you are looking . hopefully that will help you. [1]: http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android – amDroid Jan 16 '13 at 09:50
  • I've gone through the URL you provided, still unclear. – Neomat Smith Jan 16 '13 at 10:13

2 Answers2

2

The higher is the density, the smaller are the physical pixels. Therefore, to keep the size of a measure in dp to remains the same, 1dp requiert more physical pixels at a higher density because there are smaller.

The official normalisation for a dp is 1dp = 1px at a density of 160dpi; therefore, at a density of 240dpi - which is 50% greater - you need 50% more physical pixels in order to keep the same length for a measure expressed in dp.

SylvainL
  • 3,926
  • 3
  • 20
  • 24
1

Android defines density ratio for devices with different screen densities. For mdpi devices, this ratio is set to 1. So if you specify width as 1dp, android calculates the pixel value by multiplying dp value with density ratio i.e, for mdpi device, px = 1 (dp) * 1 (ratio) = 1px.

But on a high density device like xhdpi device, the ratio is 2, and android will convert dp to pixel by multiplying dp value with density ratio. px = 1 (dp) * 2 (ratio) = 2px. Thus, you element will be of 2px on xhpi device.

More info can be found here: http://www.jtechniques.com/android/android-basics/understanding-dp-in-android-ui-px-vs-dp

Harish BN
  • 15
  • 1
  • 5