为什么林特显示警告使用(英寸)或(毫米)为单位的尺寸是什么时候? [英] Why Lint shows warning when using in (inch) or mm (millimeter) units as dimension?

查看:152
本文介绍了为什么林特显示警告使用(英寸)或(毫米)为单位的尺寸是什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件明确的说,在毫米 的屏幕大小而定。

  

毫米   
毫米 - 基于屏幕的物理尺寸。

     

在   
英寸 - 基于屏幕的物理尺寸

但是,当我使用它们,林特说:

  

避免使用毫米为单位(不准确的所有设备工作);使用DP,而不是

时的文件错了吗?为什么林特不使用时发出警告 PT

  

PT
  点 - 英寸基于屏幕的物理尺寸的1/72

解决方案

要能够给出很好的回答你的问题我首先得给新闻部的工作方式在Android中的解释,所以这个答案是有点长。

TL; DR:该文件是正确的,但是从皮棉警告也是正确的。这是有一些(越野车?)设备,其中毫米 PT 工作不正常。

DPI Android中

在机器人有两种不同类型的密度措施,可以从 DisplayMetrics 的被访问类:

  • <一个href="http://developer.android.com/reference/android/util/DisplayMetrics.html#densityDpi">densityDpi - pssed为点每英寸的屏幕像素密度EX $ P $。此值始终设置为广义密度桶一个可用的Andr​​oid的(如中/ 160,高/ 240,超高/ 320)。
  • xdpi /的 ydpi - 每次在X / Y尺寸屏幕英寸的确切物理像素。这并不罕见,这值比densityDpi稍有不同这取决于屏幕尺寸和分辨率的装置。有

DP 单元是基于densityDpi指标计算的,而毫米 PT 单位所有计算的基础上xdpi / ydpi代替。

报告值

有可能的分割装置分为四个不同的类别取决于什么值他们报告为densityDpi和xdpi / ydpi:

  1. densityDpi几乎是相同的,或相同xdpi / ydpi。
  2. densityDpi明显比xdpi / ydpi
  3. densityDpi明显比xdpi / ydpi
  4. 的xdpi / ydpi值是完全错误的。

类别1(densityDpi相同xdpi / ydpi)

示例设备:三星Galaxy趋势方面,Nexus 4,Nexus 7保护

因为有这些设备,并没有真正如果你使用毫米或<$ C无所谓$ C> DP 。如果你画一个边 1的正方形在和一个与 160 DP ,他们将是相同的,都将是约1英寸的实际屏幕上。

类别2及3(densityDpi比xdpi / ydpi不同)

2类设备例如:的HTC One S,索尼Xperia V,索尼XperiaŽ超
第3类设备例如:三星Galaxy S4,索尼Xperia Z1,Nexus的5

有关这些装置的densityDpi和xdpi / ydpi不同,因为该densityDpi必须被设置为可用的密度桶中的一个。所以真正的物理dpi的舍入相匹配的最近的桶,这就是被报告为densityDPI值。这是由图1中支持多个屏幕的文件中说明。

这意味着,如果你画同方方1 和一个与 160 DP ,他们将有小幅不同的尺寸。对于2类设备的 160 DP 广场将是更小,而且第3类设备,它会直接通过略大。如果你拿出一把尺子和物理测量方块手机的屏幕上,你将看到 1 广场是1英寸,而 160 DP 广场是一个有点小或更大的取决于类别。

这是Android中密度桶系统的自然结果,但除非你有它的工作原理是一个更深入的理解可能有点令人惊讶的事情得出一个固定的DP措施可能看起来在不同的手机不同。

第4类(不正确xdpi / ydpi值)

示例设备:三星Galaxy迷你,三星Galaxy S3迷你

这些设备是更多的问题,他们良好的价值densityDpi(他们的真实DPI四舍五入到最接近桶)一有,但他们报告的xdpi / ydpi假值。

例如三星Galaxy S3迷你报道了160和240。屏幕480像素宽densityDpi的xdpi因此,如果160 dpi的是正确的,这将意味着3英寸的屏幕宽度,但在现实中与屏幕为2.05英寸。根据这些数字的实际xdpi值的手机应该是报告为234。

又如三星Galaxy赠送其中还报告的160的xdpi,但densityDpi设定为120,即画面是240像素宽,使得将给予的1.5英寸的物理宽度,但在现实中,屏幕是1.9英寸(实际126 xdpi)。

所以,对这些类型的设备,不可能信任的,毫米,角台,因为它们会导致事情都是这样过小或太大了。

示例

类别1-3

这是一个测试应用程序,我所采取的设备从1类,2和3了三个截图绿色广场被吸引到1英寸的大基于使用xdpi和ydpi值,而红色方块绘制使用DP(160 DP)为1英寸的大。

在实际测量结果与统治者的绿色方块是寸大在所有这些例子,而红色方块的大小有所不同,因为它是在截图可见。

第4类

这是从4类设备的屏幕截图。绿色和红色的方块是一样的在previous例子,但在这里,我还增加了两个正方形,一个黄色正方形,即 1 和蓝色方即 72 PT 。由于在截图可见,除了红场所有方块大小相等。

在实际测量结果用尺子红场约一英寸的大而平方其余都只有0.67英寸的大。

的问题的答案

这是皮棉的警告是指返回不正确的值xdpi和ydpi 4类类型的设备。由于xdpi / ydpi不能信任这些设备的单位(毫米 PT )依赖于它们不能信任任何。

  

时的文件错了?

,这些单位是根据屏幕的物理尺寸否的文档是正确的。然而,一些有问题的设备报告错误价值为屏幕的实际尺寸(xdpi / ydpi)到Android,然后这些单位将是错误的为好。

  

和皮棉为什么不使用PT时发出警告?

皮棉开发商可能只是忘了 PT ,它只是作为问题的,因为毫米

皮棉建议使用其原因 DP 反而是因为这是一种用于非常频繁的Andr​​oid因此,如果任何设备打破了单位 DP 单位pretty的多所有的应用程序看起来可怕,之前该装置是以往任何时候发布上市,将是固定的。

Documentation clearly says that mm and in are screen size dependent.

mm
Millimeters - Based on the physical size of the screen.

in
Inches - Based on the physical size of the screen.

But when I use them, Lint says:

Avoid using "mm" as units (it does not work accurately on all devices); use "dp" instead

Is documentation wrong? And why Lint doesn't warn when using pt?

pt
Points - 1/72 of an inch based on the physical size of the screen.

解决方案

To be able to give a good answer to your question I first have to give an explanation on how DPI works in Android, so this answer is a bit long.

TL;DR: The documentation is correct, however the warning from Lint is also correct. That is there are some (buggy?) devices where mm, in and pt doesn't work correctly.

DPI in Android

In Android there are two different kinds of density measures that can be accessed from the DisplayMetrics class:

  • densityDpi - The screen density expressed as dots-per-inch. This value is always set to one of the generalized density buckets available in Android (e.g. Medium/160, High/240, Extra High/320).
  • xdpi / ydpi - The exact physical pixels per inch of the screen in the X/Y dimension. It is not uncommon that this value is slightly different than densityDpi depending on what screen size and resolution the device have.

The dp unit is calculated based on the densityDpi metric while the mm, in and pt units are all calculated based on xdpi/ydpi instead.

Reported values

It is possible to divide devices into four different categories depending on what values they report for densityDpi and xdpi / ydpi:

  1. densityDpi is almost the same, or the same as xdpi / ydpi.
  2. densityDpi is noticeably smaller than xdpi / ydpi
  3. densityDpi is noticeably larger than xdpi / ydpi
  4. the xdpi / ydpi values are completely wrong.

Category 1 (densityDpi is the same as xdpi / ydpi)

Example devices: Samsung Galaxy Trend, Nexus 4, Nexus 7.

For these devices there it doesn't really matter if you use in, mm, or dp. If you draw a square with sides 1 in and one with 160 dp they will be the same and both will be about 1 inch on the actual screen.

Category 2 & 3 (densityDpi is different than xdpi / ydpi)

Category 2 example devices: HTC One S, Sony Xperia V, Sony Xperia Z Ultra.
Category 3 example devices: Samsung Galaxy S4, Sony Xperia Z1, Nexus 5.

For these devices the densityDpi and xdpi / ydpi differs because the densityDpi must be set to one of the available density buckets. So the true physical dpi is rounded to match the nearest bucket and this is what is reported as densityDPI value. This is illustrated by Figure 1 in the Supporting Multiple Screens document.

This means that if you draw a square with side 1 in and one with 160 dp they will have slightly different size. For category 2 devices the 160 dp square will be slightly smaller and for category 3 devices it will instead by slightly larger. If you take out a ruler and physically measure the squares on the screen of the phone you will see that the 1 in square is 1 inch while the 160 dp square is a bit smaller or bigger depending on category.

This is a natural consequence of the density buckets system in Android, but unless you have a deeper understanding of how this works it may be a bit surprising that things drawn with a fixed dp measure may look different on different phones.

Category 4 (incorrect xdpi / ydpi values)

Example devices: Samsung Galaxy Mini, Samsung Galaxy S3 Mini.

These devices are more problematic, they have a good value for densityDpi (their true dpi rounded to the nearest bucket), but they report bogus values for xdpi / ydpi.

For example Samsung Galaxy S3 Mini reports an xdpi of 160 and densityDpi of 240. The screen 480 px wide so if 160 dpi was correct this would mean a screen width of 3 inch, but in reality the screen with is 2.05 inch. Based on these numbers the actual xdpi value that the phone should be reporting is 234.

Another example is the Samsung Galaxy Mini which also reports an xdpi of 160, but has densityDpi set to 120. That screen is 240 px wide so that would give a physical width of 1.5 inch, but in reality the screen is 1.9 inch (actual xdpi of 126).

So on these types of devices it is not possible to trust the in, mm, pt units since they will result in things being way too small or way too big.

Examples

Category 1-3

This is three screenshots of a test app I've made taken on devices from category 1, 2 and 3. The green square is drawn to be 1 inch big based on using the xdpi and ydpi values while the red square is drawn to be 1 inch big using dp (160 dp).

When physically measuring the result with a ruler the green square was inch big in all these examples while the red square size differed somewhat as it's visible on the screenshots.

Category 4

This is a screenshot from a category 4 device. The green and red squares are the same as in the previous example, but here I have also added two more squares, a yellow square that is 1 in and a blue square that is 72 pt. As visible on the screenshot all squares except the red square are of equal size.

When physically measuring the result with a ruler the red square is about one inch big while the rest of the squares are only about 0.67 inch big.

The answer to the question

The warning from Lint refers to category 4 type devices that return incorrect values for xdpi and ydpi. Since xdpi / ydpi can not be trusted on these devices the units (mm, in, pt) that depend on them can not be trusted either.

Is documentation wrong?

No the documentation is correct, these units are based on the physical size of the screen. However some problematic devices report wrong values for the physical size of the screen (xdpi / ydpi) to Android and then these units will be wrong as well.

And why Lint doesn't warn when using pt?

The Lint developers probably just forgot about pt, it is just as problematic as in and mm.

The reason Lint recommends using dp instead is because this is a unit that is used extremely frequently in Android so if any device had broken dp units pretty much all applications would look horrible and it would be fixed before the device was ever released to market.

这篇关于为什么林特显示警告使用(英寸)或(毫米)为单位的尺寸是什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆