如何将 Android 图像的 ppi 转换为 dpi?
我已经开始使用 Adobe Photoshop 为我的 Android 应用程序制作图形。但我无法继续,因为 Photoshop 中的分辨率是以每英寸像素为单位设置的,而 Google 官方文档称 Android 需要以 dpi 为单位设置图像。我在网上搜索了两者之间的转换,但从未找到任何合适的公式。
我知道 Android 文档将这种关系描述为 px = dp*dpi/160 。但我的问题是,如果我知道 dpi,我从哪里获得用于此计算的 dp 值?或者说dp的值有什么假设吗?我很困惑。
I have started making graphics for my Android app using Adobe Photoshop. But I am unable to proceed, as the resolution in Photoshop is set in pixels per inch where as the official Google documentation says Android will require images set in dpi. I have searched the web for the conversion between the two but never ended up with any proper formula.
I know that the Android documentation describes the relation as px = dp*dpi/160
. But my problem is that if I know dpi where do I get the value of dp to be used in this calculation? Or is there any assumption about the value of dp? I am confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Dp 是与密度无关的像素,用于概括屏幕具有的像素数量。这些是取自 http://developer.android.com/guide/practices/screens_support 的概括数字.html
屏幕的通用 Dpi 值:
因此,您的资源的广义大小(假设它们是全屏):
编辑 - 添加 xhdpi,因为它们变得越来越流行
这些值应该适合大多数 xhdpi 屏幕,例如电视和 Nexus 4,包括 Nexus 10(假设它们不创建这是一个新类别,因为它是 25k x 16k,不知道,因为我没有手还没有上一个)。
/编辑
如果您使用这些尺寸,您的图像在任何屏幕上都会看起来很棒。请务必在代码中以 dp 定义大小,但是 Android 将自行处理上述转换。
Dp are Density independant pixels and are used to generalise the number of pixels a screen has. These are generalised figures taken from http://developer.android.com/guide/practices/screens_support.html
Generalised Dpi values for screens:
Therefore generalised size of your resources (assuming they are full screen):
Edit - adding xhdpi as they are becoming more popular
These values should be suitable for most xhdpi screens such as TVs and the Nexus 4, including the Nexus 10 (assuming they don't create a new category for this as it is 25k x 16k, don't know as I haven't got hands on one yet).
/Edit
If you use these sizes your images will look great on any screen. Be sure to define sizes in code in dp however, Android will handle the conversion described above on its own.
我不同意 Michael Allen 的回答,因为 ldpi、mdpi、hdpi 和 xdpi 的最终分辨率不满足 google 文档中“替代绘图”下提到的替代位图的 3:4:6:8 缩放比例
http://developer.android.com/guide/practices/screens_support.html#testing
因此,我建议您
现在采用最小尺寸为 470 x 320 dp 的基准示例使用同一文档中的公式,我们计算基准屏幕尺寸的全屏分辨率
px = dp * (dpi/160);对于基线 px = dp * (160/160) = dp * 1
所以 px = dp。这意味着我们的基准配置的全屏尺寸。以像素为单位现在为
470 X 320 px (mdpi),
为了遵循 ldpi、hdpi 和 xhdpi 的替代可绘制尺寸的 3:4:6:8 缩放比例,我们需要从 mdpi 导出单位值。即
470/4 = 117.5
320/4 = 80
除以 4,因为 mdpi 的缩放比为 4,ldpi、hdpi 和 xhdpi 的缩放比分别为 3,6 和 8。现在只需将单位结果 117.5 和 80 与这些缩放因子相乘
ldpi
117.5 * 3 = 352.5
80 * 3 = 240
mdpi
117.5 * 4 = 470
80 * 4 = 320
hdpi
117.5 * 6 = 705
80 * 6 = 480
xhdpi
117.5 * 8 = 940
80 * 8 = 640
这些尺寸现在具有完美的 3:4:6:8 缩放比例。
I don't agree with answer by Michael Allen because the resulting resolutions for ldpi, mdpi, hdpi and xdpi doesn't satisfy the 3:4:6:8 scaling ratios for alternative bitmaps mentioned in the google docs here under 'Alternative Drawables'
http://developer.android.com/guide/practices/screens_support.html#testing
Therefore I would suggest that you take the baseline example that has a minimum size of
470 x 320 dp now using formula from same documentation we calculate full screen resolution for baseline screen size
px = dp * (dpi/160); for baseline px = dp * (160/160) = dp * 1
so px = dp. This means the full screen size for our baseline config. in pixels would be
470 X 320 px (mdpi)
now to follow the 3:4:6:8 scaling ratios for alternative drawables sizes for ldpi, hdpi and xhdpi we need to derive the unit values from mdpi. i.e.
470/4 = 117.5
320/4 = 80
divide by 4 because scaling ratio for mdpi is 4, scaling ratios for ldpi, hdpi and xhdpi are 3,6 and 8 respectively. now just multiply the unit results 117.5 and 80 with these scaling factors
ldpi
117.5 * 3 = 352.5
80 * 3 = 240
mdpi
117.5 * 4 = 470
80 * 4 = 320
hdpi
117.5 * 6 = 705
80 * 6 = 480
xhdpi
117.5 * 8 = 940
80 * 8 = 640
These sizes are now in perfect 3:4:6:8 scaling ratios.
是的,Dp(密度独立像素)是 Photoshop 所关心的。它不应该考虑像素的密度。超高像素的图像可以放在一个超小的高清 UI 按钮上,以实现它所关心的一切。或者你可能只有很少的像素来制作一张复古风格的俄罗斯方块A0海报。这是因为它们可以缩放。屏幕上的大区域很少有像素被放大,而小区域的许多像素被缩小。
因此,请选择您的目标尺寸链接屏幕尺寸并在 Photoshop 中使用它。 的尺寸饼图)
(另请查看谁使用什么...使用 这个网站的直觉帮助我链接到博客页面:
(平台进行缩放 - 密度比处理是由 Android 完成的;而不是由您完成),这与 UI 极其相关:
因为您希望 UI 元素在所有设备上显示相同,所以它将根据在该公式中计算的像素数进行缩放。如果 dp 的缩放方式使其变得过于稀疏或密集,那么看起来就不太好。
Yes, Dp (Density independant pixels) is all Photoshop will care about. It should not consider what the density of the pixels will be. That image of a super high number of pixels may be put on a super small high definition UI button for all it cares about. Or you may have few pixels for a retro style A0 poster of Tetris. This is because they can get scaled. Few pixels are scaled up for the large area, and many pixels scaled down for the small area on a screen.
So pick your target link to sizes screen size and use that in photoshop. (also check out who uses what... pie chart of sizes used)
For the intuition this site helped me link to blog page:
(the platform does the scaling- the density ratio handling is done by Android; not you) and this is extremely relevant to the UI:
Because you want the UI elements to appear the same on all devices so it will do scaling based on the number of pixels it computes in that formula. And if the dp is scaled in a way so that it becomes too spare or dense, well that won't look good.
由于 Photoshop 中的分辨率是以每英寸像素为单位设置的,而 Google 官方文档称 Android 需要以 dpi 为单位设置图像。
As the resolution in Photoshop is set in pixels per inch where as the official Google documentation says Android will require images set in dpi.
在所有设备上都是相同的,因此它将根据在该公式中计算的像素数进行缩放。
The same on all devices so it will do scaling based on the number of pixels it computes in that formula.