Android 中字体的物理大小(以点为单位)

发布于 2024-09-12 10:39:19 字数 345 浏览 2 评论 0原文

我有两台设备 - HTC Tattoo 和索尼爱立信 Xperia X10。一个具有 145 DPI,另一个具有 245 DPI。

当我以点为单位指定 TextView 的字体大小时,如下所示:

textView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 6.5f);

我在这两个设备上得到不同的文本物理大小。在 245 DPI 下,文本几乎难以辨认。

现在,“pt”大小应该是物理大小。即,在我的测试中,两个文本块应该具有相同物理高度的字母。事实并非如此。

这里可能出了什么问题?

感谢您的帮助, 尤里。

I have two devices - HTC Tattoo and Sony Ericsson Xperia X10. One has 145 DPI, the other 245 DPI.

When I specify font size for a TextView in points, like this:

textView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 6.5f);

I get different physical size of text on these two devices. On 245 DPI, the text is barely readable.

Now, "pt" size is supposed to be physical. I.e., in my test, both text blocks should have letters of the same physical height. Which is not the case.

What can be wrong here?

Thanks for the help,
Yuri.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪花飘飘的天空 2024-09-19 10:39:19

pt 单位是点,不会根据密度缩放。对于非文本,您需要 dip (或 dp),它们是与密度无关的像素。这些将根据密度进行缩放。对于文本,您需要 sp ,它将根据密度以及用户首选的字体大小进行缩放。

这里有一些关于它的信息,也分散在 Android 文档的其他地方。

http://developer.android.com/guide/practices/screens_support.html #screen-independence

更新:这里是每个尺寸单位的描述,正如 Yuri 正确指出的那样,这确实表明物理屏幕上的 pt 应该始终是 1/72 英寸。它似乎不是这样工作的,答案就是使用 dp 和 sp - 如果你想向用户公开诸如点之类的东西,只需这样做一些数学知识(在 160dpi 屏幕上,dp 可以假设为 1px,即 1/160 英寸)。

http://developer.android.com/guide/topics/resources /more-resources.html#Dimension

The pt unit is points, which will not scale according to density. For non-text you want dip (or dp), which are density independant pixels. These will scale according to density. For text, you want sp, which will scale according to density but also according to the user's preferred font size.

There's a little info on it here, and also scattered elsewhere in the Android docs.

http://developer.android.com/guide/practices/screens_support.html#screen-independence

UPDATE: here are descriptions of each of the dimension units, which as Yuri rightly points out does suggest that pt should always be 1/72 of an inch on the physical screen. It just doesn't appear to work that way, and the answer is just to use dp and sp - if you want to expose something like points to your user, just do some maths (dp can be assumed to be 1px on a 160dpi screen, i.e. 1/160th of an inch).

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

假扮的天使 2024-09-19 10:39:19

实际上,只要底层设备提供正确的显示指标,点总是有效的。

例如,我测试了五台设备:

NAME      OS    metrics.ydpi  REAL_DPI  DDPI  SD
XPERIA    1.6   159.49677     325       240   1.5
Liquid    1.6   263.8958      285       240   1.5
Tattoo    1.6   145.14067     match     120   0.75
Hero      1.5   179.29352     match     -     1.0
Galaxy    1.6   160.41878     180       160   1.0

可以看出,只有 HTC Tattoo 和 HTC Hero 向 Android API 提供了正确的显示信息。

这就是为什么点在不同设备上具有不同的物理尺寸(但不是全部)。

我发现粗略缩放字体/图像的唯一方法是假设所有内容都是 160 DPI 并使用 DisplayMetrics#scaledDensity - 这是错误的,但我没有看到任何其他方法。

Actually, points always work whenever the underlying devices gives correct display metrics.

For example, I tested five devices:

NAME      OS    metrics.ydpi  REAL_DPI  DDPI  SD
XPERIA    1.6   159.49677     325       240   1.5
Liquid    1.6   263.8958      285       240   1.5
Tattoo    1.6   145.14067     match     120   0.75
Hero      1.5   179.29352     match     -     1.0
Galaxy    1.6   160.41878     180       160   1.0

As can be seen, only HTC Tattoo and HTC Hero give proper display info to Android API.

This is why points have different physical sizes on different devices (not all, though).

The only way I found to roughly scale fonts / images is to assume everything is 160 DPI and use DisplayMetrics#scaledDensity - which is wrong, but I don't see any other way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文