27
votes

I was reading dp, dip, px, sp measurements, but I still have some questions about dp/dpi vs ppi vs px vs inch. I am not able to compare them... is an inch the largest?

They say 160 dpi means 160 pixels per one inch. Does that mean 1 inch contains 160 pixels?

They also say 1 pixel on a 160 dpi screen = 1 dp. Does that mean 1 pixel and 1 dp are equal?

And lastly, why should we use dp instead of px? I understand that it is ideal, but why?

6
Remember that a lot of android screens are different resolutions and difference densities, and see if this helps: stackoverflow.com/q/2025282/324625Steve Blackwell
@steve blackwell: i was reading that! but i am not able to draw comparision :\Chandeep

6 Answers

46
votes

You should (almost) always use flexible sizing units, like dp, which is Density-Independent Pixels, because 300px on one device is not necessarily the same amount of screen real estate as 300px on another. The biggest practical implication is that your layout would look significantly different on devices with a different density than the one your design targeted.

  • dp or dip means Density-independent Pixels
  • dpi or ppi means Dots (or Pixels) Per Inch
  • inch is a physical measurement connected to actual screen size
  • px means Pixels — a pixel fills an arbitrary amount of screen area depending on density.

For example, on a 160dpi screen, 1dp == 1px == 1/160in, but on a 240dpi screen, 1dp == 1.5px. So no, 1dp != 1px. There is exactly one case when 1dp == 1px, and that's on a 160dpi screen. Physical measurement units like inches should never be part of your design—that is, unless you're making a ruler.

A simple formula for determining how many pixels 1dp works out to is px = dp * (dpi / 160).

9
votes

dp is a physical measurement like inches. (Yes, it is. Read on.)

"A dp corresponds to the physical size of a pixel at 160 dpi" (https://developer.android.com/training/multiscreen/screendensities.html#TaskUseD)

The physical size of a pixel at 160 dpi is exactly 1/160th of an inch. Therefore the size of a dp is 1/160th of an inch. 160 dp = 1 inch.

Px is a somewhat arbitrary unit of measurement on a screen.

For examples of what dp converts to in px on different devices, see here:

https://stackoverflow.com/a/39495538/984003

6
votes

How do dp, dip, dpi, ppi, pixels and inches relate?

For the purpose of android development:

dp = dip
dpi = ppi
inch x dpi = pixels
dp = 160 x inch
dp = 160*pixels/dpi

So, on a 160dpi phone (mdpi):

2 inches = 320 dp
2 inches = 320 pixels

On a 180 dpi phone:

2 inches = 320 dp
2 inches = 360 pixels

Note that 2 inches is ALWAYS 320dp, independent of screen size. A dp is a physical distance of 1/160th of an inch.

The dp to pixels formula is interesting:

dp = 160*pixels/dpi

Is equivalent to:

dp = pixels/(dpi/160)

dpi/160 is an interesting factor. Its the relative density compared to android's mdpi bin and the amount you must scale your graphics by for the various resource bins. You'll see that factor mentioned a few times on this page, 0.75 being the factor to ldpi.

1
votes

I will explain using an example.

float density = context.getResources().getDisplayMetrics().density;
float px = someDpValue * density;
float dp = somePxValue / density;

density equals

.75 on ldpi (120 dpi)
1.0 on mdpi (160 dpi; baseline)
1.5 on hdpi (240 dpi)
2.0 on xhdpi (320 dpi)
3.0 on xxhdpi (480 dpi)
4.0 on xxxhdpi (640 dpi)

so for example,

I have a Samsung S5 with 432 dpi (http://dpi.lv/#1920×[email protected]).

So, density = 432/160 = phone's dpi/baseline = 2.7

Let say my top bar is 48dp. This is referenced to baseline (160dpi).

So, w.r.t my S5, it will be 48dp * 2.7.

Then if I want to see the actual height:

It will be (48dp * 2.7) / 432 dpi = 0.3 inches.

0
votes

DP is the resolution when you only factor the physical size of the screen. When you use DP it will scale your layout to other similar sized screens with different pixel densities.

Occasionally you actually want pixels though, and when you deal with dimensions in code you are always dealing with real pixels, unless you convert them.

0
votes

When not referring to android, but rather to monitors, DP actually means Dot Pitch, which originally came about from CRT monitors. It refers to the diagonal distance between 2 pixels in mm. In LCD monitors, a pixel is larger, and assuming the pixels are right next to each other without gap (usually there is a very small gap, but for simplicity, we will assume it is zero), the diagonal distance between the 2 centers of each pixel is equal to the diagonal size of the pixel. The lower the DP, the crisper the image.

DP = 25.4÷ppi 0.25 DP is standard, jagged edge 0.20 DP is considered much clearer 160 ppi = 0.158 DP

So DiP is actually a rounded approximation of 1000 x DP, and the 2 are not equivalent, just very close approximations.

As mentioned before, you should not base things off of pixel size since you can zoom. This is for how clear something will appear on the screen.

In monitors, if you want clarity at 20 inches (average distance between monitor to eye) (< 0.20 DP medium clarity/0.16 DP ultra sharp), this would equate to: 1920x1080 (HD) 17.4 inch/ 14 inch 3840x2160 (4K) 35 inch / 27.8 inch A high resolution phone might have a DP of 0.05 (approximately 500 ppi), or 3 times higher than that of a ultra sharp monitor but viewed 3 times closer.

Anything larger than these dimensions for the monitor size will appear pixelated, smaller would be clearer.

Also noteworthy is that 72 pixels per inch is a Mac standard, and very old. 96 ppi is what Windows resolution is referenced to. Photoshop was originally designed for the Mac.