Sébastien
Sébastien

Reputation: 14821

Why "dp-to-pixel" ratio changes with the screen density, but not necessarily in direct proportion

I am a bit worried by what I read here, which kind-of implies that there is no reliable formula to compute px based on dp and screen density (and vice versa):

dp : Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.

I thought we could always use the following formula (explained here) : px = dp * (dpi / 160)

However, the testimony in this question seems to highlight a case where specifying sizes in dp does not guarantee a fixed perceived size across devices.

What is the real meaning of the sentence in bold ? Do we need to use mm in order to be sure to keep the same perceived size ??

Upvotes: 1

Views: 2054

Answers (1)

nibarius
nibarius

Reputation: 4117

I thought we could always use the following formula (explained here) : px = dp * (dpi / 160)

This formula can always be used to convert between pixels and dp. Just make sure you use DisplayMetrics.densityDpi as the value for dpi in the formula.

However, the testimony in this question seems to highlight a case where specifying sizes in dp does not guarantee a fixed perceived size across devices.

Yes it is true that a certain dp does not guarantee a fixed perceived size across devices. It does however guarantee a similar perceived size across devices. The reason for this is that on some devices densityDpi (the density bucket) is different than the physical dpi of the device. Please see my answer to the question you linked for more details.

The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion

Here I think they mean the actual physical screen density when they say "screen density" and not which density bucket the phone belongs to.

For example a device with a physical dpi of 270 will belong to the "high" (240) density bucket and have a dp-to-pixel ratio of 240/160 = 1.5

Another device with a physical dpi of 290 will belong to the "xhigh" (320) density bucket and have a dp-to-pixel ratio of 320/160 = 2

When you compare these devices with each other the physical screen density has increased by 7% (270 -> 290) while the dp-to-pixel ratio has increased by 33% (1.5 -> 2) so it's not a direct proportion between them.

Do we need to use mm in order to be sure to keep the same perceived size ??

If it's important that the preceived size is as similar as possible between devices you have to use mm or inch. However this have some drawbacks as I mention in the question you linked.

It is also important to keep in mind that there are some devices where the mm and inch units are broken, for more information on this see the question Why Lint shows warning when using in (inch) or mm (millimeter) units as dimension?

Upvotes: 3

Related Questions