记录思考

在 Android 中,“px”, “dip”, “dp” 和 “sp” 有哪些不同

在 Android 中,“px”, “dip”, “dp” 和 “sp” 有哪些不同

问题

在 Android 中,“px”, “dip”, “dp” 和 “sp” 有哪些不同?在 Android 各个单位中,有哪些不同。

个人观点

这是一个 Android 入门的问题,对于很多刚入门 Android 开发的开发者来说。这几个单位是必须需要搞明白的。但是这些知识点并不是 Android 特有的,对于之前从事其它平台开发的开发者来说,这些并不陌生。这个问题来着 Stackoverflow,是一个比较高票的问答。

从大部分回答上可以看到,阅读 Android develpoer 官方文档是多么的重要。而这个事情被很多国内的开发者忽略掉了,我发现身边的很多同事并没有阅读官方文档的习惯,被墙和英文是一个比较大的原因,但我不认为是理由。办法总是比问题多的嘛,就像 stackoverflow 上,答案总是比问题多。

答案

以下来自 Android 开发者官方文档。 From the Android Developer Documentation:

From Understanding Density Independence In Android:

+----------------+----------------+---------------+-------------------------------+
| Density Bucket | Screen Density | Physical Size | Pixel Size                    |
+----------------+----------------+---------------+-------------------------------+
| ldpi           | 120 dpi        | 0.5 x 0.5 in  | 0.5 in * 120 dpi = 60x60 px   |
+----------------+----------------+---------------+-------------------------------+
| mdpi           | 160 dpi        | 0.5 x 0.5 in  | 0.5 in * 160 dpi = 80x80 px   |
+----------------+----------------+---------------+-------------------------------+
| hdpi           | 240 dpi        | 0.5 x 0.5 in  | 0.5 in * 240 dpi = 120x120 px |
+----------------+----------------+---------------+-------------------------------+
| xhdpi          | 320 dpi        | 0.5 x 0.5 in  | 0.5 in * 320 dpi = 160x160 px |
+----------------+----------------+---------------+-------------------------------+
| xxhdpi         | 480 dpi        | 0.5 x 0.5 in  | 0.5 in * 480 dpi = 240x240 px |
+----------------+----------------+---------------+-------------------------------+
| xxxhdpi        | 640 dpi        | 0.5 x 0.5 in  | 0.5 in * 640 dpi = 320x320 px |
+----------------+----------------+---------------+-------------------------------+

单位:

+---------+-------------+---------------+-------------+--------------------+
| Unit    | Description | Units Per     | Density     | Same Physical Size |
|         |             | Physical Inch | Independent | On Every Screen    |
+---------+-------------+---------------+-------------+--------------------+
| px      | Pixels      | Varies        | No          | No                 |
+---------+-------------+---------------+-------------+--------------------+
| in      | Inches      | 1             | Yes         | Yes                |
+---------+-------------+---------------+-------------+--------------------+
| mm      | Millimeters | 25.4          | Yes         | Yes                |
+---------+-------------+---------------+-------------+--------------------+
| pt      | Points      | 72            | Yes         | Yes                |
+---------+-------------+---------------+-------------+--------------------+
| dp      | Density     | ~160          | Yes         | No                 |
|         | Independent |               |             |                    |
|         | Pixels      |               |             |                    |
+---------+-------------+---------------+-------------+--------------------+
| sp      | Scale       | ~160          | Yes         | No                 |
|         | Independent |               |             |                    |
|         | Pixels      |               |             |                    |
+---------+-------------+---------------+-------------+--------------------+

更多可以参考 Google Design Documentation.

这里有一个应用可以计算真实设备的分辨率 this

答案

这里有一遍来自 Android Developer 的文档,多屏幕适配 Supporting Multiple Screens

If you are any serious about developing an Android app for more than one type of device, you should have read the screens support development document at least once. In addition to that it is always a good thing to know the actual number of active devices that have a particular screen configuration.

- EOF -

本文链接 https://spacepage.top/articles/2018.05.22-android_difference_between_dp_px_sp.html,欢迎转载,转载请注明出处。