HeightPixels / Density 实际上代表什么?

What does HeightPixels / Density actually represent?

我正在考虑根据我的设备大小设置我的应用程序的 FontSize。即如果设备小于 8",则使字体小于默认值。

所以我使用的设备是 Lenovo S8-50 8-Inch Tablet 正如您在规格中看到的 Screen Size: 8 inches

所以查看 Github 上的一些代码,我看到以下内容:

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

对于这个设备,返回的数字是 912,我不知道这个数字与设备有什么关系。

所以我的问题是这个数字实际上代表什么?

来自官方文档:

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.

这也会有帮助:

Understanding density independence