如何找到 phone 所在的密度桶?

How to find what density bucket the phone is in?

有点菜鸟问题,但在计算 phone 属于哪个密度桶时我感到困惑。我将以我的 Galaxy S3 为例。

它的分辨率为 1280 x 720,这意味着它有 306 dpi。现在,参考下面的图表,我的 phone 属于 hdpi 类别,因为它超过 240 dpi 但少于 320 dpi 需要成为 xhdpi ] 屏幕。所以,phone 是 853 x 480 DP(除以 1.5)

但是,我 phone 上的屏幕信息应用程序告诉我这是一个 xhdpi 屏幕。所以,phone 是 640 x 360 DP(除以 2)。

我怎么知道我的 phone 属于哪个正确的密度桶?

更新:
我正在尝试为我所在国家/地区的前 10 Android phone 设计我的应用程序。因此,我正在计算它们在 DP 中的大小,以根据它们 "smallest width DPs" 设计 UI。这不是一次性的尺寸计算。

如果你想一次了解它,你可以得到一个像ScreenInfo和运行这样的应用程序。如果你想以编程方式知道,你可以添加密度文件夹,例如values-mdpivalues-xhdpi,在其中放置一个xml文件,比如whoami.xml,内容如下:

<resources>
    <string name="density_bucket">mdpi</string>
</resources>

并且分别是:

<resources>
    <string name="density_bucket">xhdpi</string>
</resources>

然后从应用程序查询:

String densityBucket = getResources().getString( R.string.density_bucket );

我认为 Android 试图找到 "best match"。所以如果 306 在 240 和 320 之间,它更接近 320。所以它将使用 xhdpi。您可以阅读更多 here。引用官方 link 到文档:

Based on the size and density of the current screen, the system uses any size- and density-specific resource provided in your application. For example, if the device has a high-density screen and the application requests a drawable resource, the system looks for a drawable resource directory that best matches the device configuration. Depending on the other alternative resources available, a resource directory with the hdpi qualifier (such as drawable-hdpi/) might be the best match, so the system uses the drawable resource from this directory.

嘿,我认识那个图表!主要是因为我 created it :D。澄清一下,该图表显示了每个密度桶的基线 dpi,但不能用于确定设备应归类为哪个密度桶。也就是说,下图显示了设备如何归纳到密度桶中,您可以看到它们的 dpi 可能高于或低于它们所在的桶。

现在回答您的问题,遗憾的是,无法保证仅根据其规格了解设备将属于哪个密度桶。这是因为设备制造商可以选择密度桶。大多数时候,他们会选择最接近设备实际 dpi 的桶**。幸运的是,Google 收集了 list of common devices,以及 dp 中的屏幕尺寸和密度桶。这应该让您了解如何正确支持野外最流行的屏幕尺寸。

**注意:一些设备有自己特殊的密度桶。最值得注意的是,Nexus 7 (2012) 为 213dpi (tvdpi),Nexus 5X 为 420dpi,Nexus 6 和 6P 为 560dpi。这些设备从其他密度版本获取资产并对其进行缩放。