Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relations between dip, px and dpi

If, in a layout xml file, I set the size to be, for example 12dip. Will that always be 12px in mdpi and so 18px in hdpi?

So is dip always true for mdpi and will scale accordingly for other densities?

like image 563
Espen Avatar asked Dec 23 '10 09:12

Espen


People also ask

What is the difference between px dip dp SP?

PX: is an abbreviation for Pixels, which specifies the actual pixels on the screen. SP: is an abbreviation for Scale independent pixels. It is the same as the dp unit, but it is additionally scaled according to the user's font size selection.

Is dp and DPI the same?

In Android, we have a baseline density of 160 dots-per-inch(dpi). So, for a 160 dpi screen, we have 1 pixel = 1 dp and 320 dpi screen, we have 2 pixels = 1 dp which is 2x. Let's say we have a tablet of 1280*800 pixels, 160 dpi and phone of 800*1280 pixels, 320 dpi. Their pixel resolution is the same.

How many pixels is 1dp?

For this mapping, 1 dp is considered to be equal to 1 pixel on a 160 dpi resolution screen. The corresponding number of pixels can be calculated with the formula px = dp * (dpi/160).

How do you convert pixels to dp?

The android system scales the UI component according to the pixel counts of different devices using the DP values. Pixel values can be converted to DP by dividing the pixel value by the screen pixel density.


2 Answers

That question is fully covered by official documentation. Relations between dip, px and dpi are covered by this section.

Quote:

Density-independent pixel (dp)

A virtual pixel unit that applications can use in defining their UI, to express layout dimensions or position in a density-independent way.

The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, the baseline density assumed by the platform (as described later in this document). At run time, the platform transparently handles any scaling of the dp units needed, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: pixels = dps * (density / 160). For example, on 240 dpi screen, 1 dp would equal 1.5 physical pixels. Using dp units to define your application's UI is highly recommended, as a way of ensuring proper display of your UI on different screens.

So the statement:

that always be 12px in mdpi and so 18px in hdpi

seems to be correct, according to the docs.

like image 67
Konstantin Burov Avatar answered Nov 08 '22 02:11

Konstantin Burov


12dp will be 12px on a device with density 160 dpi. The docs don't say that all mdpi devices have exactly 160 dpi so it seems you might find mdpi devices with other densities (e.g. 150 or 180 dpi). In those cases, the relation 1dp = 1px would not be true.

You can only be sure of this relation:

px = dp * (dpi / 160)

If a device has a density of 320 dpi then each dp corresponds to 2 px, because 320/160 is 2. I would say that 2 is the "density factor", but it is also what you get with getResources().getDisplayMetrics().density, so it is also called "density".

like image 29
Ferran Maylinch Avatar answered Nov 08 '22 01:11

Ferran Maylinch