设备无关像素的基础知识 [英] basics of device-independent-pixels

查看:23
本文介绍了设备无关像素的基础知识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全被 Android 上的低谷搞糊涂了.

im throughoutly confused by dips on Android.

我从参考资料中了解到 dp 值的基数是 160.那么,宽度 80dp 不应该等于宽度为屏幕 50% 的视图吗?在我的 Nexus One 上,dp 的宽度看起来大约是 300dp.

I understand from the reference that the base for dp values is 160. So, shouldn't 80dp in width equals a view with a width of 50% of the screen ? On my Nexus One the width in dp is something around 300dp as it seems.

我在这里错过了什么?

提前谢谢

推荐答案

"dp" == "Density-independent Pixels"(这也是它早先被称为dip"的原因,虽然我更喜欢使用dp"这些天.)

"dp" == "Density-independent Pixels" (This is also why it was earlier called "dip", though I prefer to use "dp" these days.)

把它想象成其他单位——in"(英寸)、mm"(毫米)等.它允许您提供一个根据屏幕密度缩放的尺寸.

Think of it like other units -- "in" (inches), "mm" (millimeters), etc. It allows you to provide a size that is scaled based on the density of the screen.

我们将 mdpi 定义为基本密度,因此 mdpi 屏幕上的10dp"将产生恰好 10 个像素.在 hdpi 屏幕上它会产生 15 个像素,因为 hdpi 是 1.5*mdpi.

We define mdpi to be the base density, so "10dp" on an mdpi screen will result in exactly 10 pixels. On an hdpi screen it will result in 15 pixels, because hdpi is 1.5*mdpi.

请注意,尽管各种密度的常数与 DPI 相似(mdpi 为 160 等),但密度完全是 DPI.它是一个抽象的缩放因子,可根据屏幕 dpi 进行调整,但不会尝试准确反映它.(您可以使用in"、mm"等精确尺寸,但 99.9% 这不是您想要的,所以坚持使用dp".)这大大简化了每个人的生活,因为您不需要处理许多 Android设备的 UI 空间量略有不同,因为它们的屏幕 DPI 略有不同.此外,设备制造商可以选择其设备的密度以实现所需的 UI——例如,Samsung Tab 使用的密度比实际 DPI 大很多,从而导致整体 UI 更大.

Note that though the constants for various densities are similar to DPI (mdpi is 160, etc), density is not exactly DPI. It is an abstract scaling factor that adjusts for screen dpi, but does not try to exactly reflect it. (You would use "in", "mm", etc for exact sizes but 99.9% that is not what you want so stick with "dp".) This greatly simplifies life for everyone because you don't need to deal with many Android devices having a slightly different amount of space for its UI because they each of slight different screen DPIs. Also, device manufacturers can select the density of their device to achieve a desired UI -- for example the Samsung Tab uses a density that is a fair amount larger than the actual DPI, resulting in an overall larger UI.

这篇关于设备无关像素的基础知识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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