What does HeightPixels / Density actually represent?

207 views Asked by At

I am looking at setting the FontSize of my application based on how big my device is. i.e. If the device is below 8" make the font smaller than the default.

So the device I am using is a Lenovo S8-50 8-Inch Tablet and as you can see in the specifications Screen Size: 8 inches

So looking at some code on Github I see the following:

var d = Resources.System.DisplayMetrics;
this.ScreenHeight = (int)(d.HeightPixels / d.Density);

For this device the number returned is 912 and I can't figure out how this number relates to the device.

So my question is what does this number actually represent?

1

There are 1 answers

0
rain_ On BEST ANSWER

From the Official documentation:

Density-independent pixel (dp) A virtual pixel unit that you should use when defining UI layout, 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, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. 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. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

This will also be helpful:

Understanding density independence