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

6

31

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?

Jakie answered 12/12, 2011 at 18:15 Comment(2)
Remember that a lot of android screens are different resolutions and difference densities, and see if this helps: https://mcmap.net/q/36057/-what-is-the-difference-between-px-dip-dp-and-sp/324625Kingcraft
@steve blackwell: i was reading that! but i am not able to draw comparision :\Jakie
O
53

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).

Otic answered 12/12, 2011 at 18:25 Comment(10)
There are many screen densities on the market, so you can certainly make use of that calculations. I'm not sure why you would, but you definitely could. For example, if you wanted to know how many pixels 300 dp would take up on a 240dpi screen, 300*(240/160) = 450 pixelsOtic
@ChrisCashwell, so when creating these image to use, what numbers to use in defining them? Use the size developer.android.com/guide/practices/ui_guidelines/… for width and height as prescribed and the resolution density?Burger
@Burger I usually make larger (higher density) images and size them in dpi units.Otic
@ChrisCashwell, can you tell me how this is done in photoshop or illustrator? I only see ppi when you start a new document.Burger
actually, 1 inch is about 160dp , so it's ok to think about it as a measurement of some kind...Succursal
Question : please can you tell me how dp is different from dpi ?Clough
@TusharPandey DP is Density-independent Pixel and DPI is Dots Per Inch. DP refers to an abstract unit based on the density of pixels on a given screen, whereas DPI refers to the number of "dots" per inch. You could derive the number of DP on a screen given the DPI value of that screen.Otic
160 dp = 1 inch exactly. From Google specs: "A dp corresponds to the physical size of a pixel at 160 dpi." The physical size of a pixel at 160 dpi is exactly 1/160th of an inch. Therefore 160 dp = 1 inch.Thiazine
@Thiazine Note the "at 160dpi" in your source article. Many devices have screen densities not equal to 160dpi.Otic
by 1dp == 1px == 1in, you mean one inch? then it of-course is wrong! 1dp == 1/160 inch exactlyClinker
T
11

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://mcmap.net/q/470681/-relation-between-dp-sp-and-px

Thiazine answered 14/9, 2016 at 16:47 Comment(2)
I am wondering why they came up with this new unit of measurement "dp" when they could have used inches or cm.Lowry
Unfortunately, equating dp with physical sizes is no longer a correct approach. Many devices now allow you to change the screen zoom level, which means that 160dp has a different physical size on each zoom level. These work by changing the "logical" screen density of the device (320dpi, 240dpi, etc). So, to be precise, 160dp is 1inch when the logical dpi matches the physical dpi (physical pixels per inch). Note that some of these devices don't default to what a 1x zoom would be. The Samsung S5e, for instance, has a physical density of ~288dpi, but the factory-default logical density is 360dpiCasino
D
8

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.

Declared answered 17/6, 2015 at 13:25 Comment(0)
P
1

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.

Pater answered 28/11, 2015 at 6:45 Comment(0)
P
0

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.

Pottage answered 12/12, 2011 at 18:24 Comment(1)
so when creating an image the resolution in pixels is the same, just that you change the physical size? So when using Photoshop, you can just stick in the default 72pixels per/inch and then have the correct width/height in pixel numbers?Burger
L
0

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.

Landreth answered 18/8, 2021 at 19:9 Comment(1)
The reason you shouldn't base things off of pixel size is because that can be a very different physical size depending on pixel density.Salable

© 2022 - 2024 — McMap. All rights reserved.