如何将 Android 图像的 ppi 转换为 dpi?

发布于 2024-11-28 00:19:37 字数 263 浏览 1 评论 0原文

我已经开始使用 Adob​​e 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 技术交流群。

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

发布评论

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

评论(5

浮生面具三千个 2024-12-05 00:19:37

Dp 是与密度无关的像素,用于概括屏幕具有的像素数量。这些是取自 http://developer.android.com/guide/practices/screens_support 的概括数字.html

  • xlarge 屏幕至少为 960dp x 720dp
  • 大屏幕至少为 640dp x 480dp
  • 普通屏幕至少为 470dp x 320dp
  • 小屏幕至少为 426dp x 320dp

屏幕的通用 Dpi 值:

  • ldpi 低密度 (ldpi) 屏幕资源 (~120dpi)
  • mdpi 中密度 (mdpi) 屏幕资源 (~160dpi)。 (这是基线密度。)
  • hdpi 高密度 (hdpi) 屏幕 (~240dpi) 的资源。
  • xhdpi 超高密度 (xhdpi) 屏幕 (~320dpi) 的资源。

因此,您的资源的广义大小(假设它们是全屏):

  • ldpi
    • 垂直 = 426 * 120 / 160 = 319.5 像素
    • 水平 = 320 * 120 / 160 = 240 像素
  • mdpi
    • 垂直 = 470 * 160 / 160 = 470 像素
    • 水平 = 320 * 160 / 160 = 320 像素
  • hdpi
    • 垂直 = 640 * 240 / 160 = 960 像素
    • 水平 = 480 * 240 / 160 = 720 像素

编辑 - 添加 xhdpi,因为它们变得越来越流行

  • xhdpi
    • 垂直 = 960 * 320 / 160 = 1920 像素
    • 水平 = 720 * 320 / 160 = 1440 像素

这些值应该适合大多数 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

  • xlarge screens are at least 960dp x 720dp
  • large screens are at least 640dp x 480dp
  • normal screens are at least 470dp x 320dp
  • small screens are at least 426dp x 320dp

Generalised Dpi values for screens:

  • ldpi Resources for low-density (ldpi) screens (~120dpi)
  • mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
  • hdpi Resources for high-density (hdpi) screens (~240dpi).
  • xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).

Therefore generalised size of your resources (assuming they are full screen):

  • ldpi
    • Vertical = 426 * 120 / 160 = 319.5px
    • Horizontal = 320 * 120 / 160 = 240px
  • mdpi
    • Vertical = 470 * 160 / 160 = 470px
    • Horizontal = 320 * 160 / 160 = 320px
  • hdpi
    • Vertical = 640 * 240 / 160 = 960px
    • Horizontal = 480 * 240 / 160 = 720px

Edit - adding xhdpi as they are becoming more popular

  • xhdpi
    • Vertical = 960 * 320 / 160 = 1920px
    • Horizontal = 720 * 320 / 160 = 1440px

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.

雨落星ぅ辰 2024-12-05 00:19:37

我不同意 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.

用心笑 2024-12-05 00:19:37

是的,Dp(密度独立像素)是 Photoshop 所关心的。它不应该考虑像素的密度。超高像素的图像可以放在一个超小的高清 UI 按钮上,以实现它所关心的一切。或者你可能只有很少的像素来制作一张复古风格的俄罗斯方块A0海报。这是因为它们可以缩放。屏幕上的大区域很少有像素被放大,而小区域的许多像素被缩小。

因此,请选择您的目标尺寸链接屏幕尺寸并在 Photoshop 中使用它。 的尺寸饼图)

(另请查看谁使用什么...使用 这个网站的直觉帮助我链接到博客页面

在运行时,平台根据使用的屏幕的实际密度透明地处理所需的 dp 单位的任何缩放。

(平台进行缩放 - 密度比处理是由 Android 完成的;而不是由您完成),这与 UI 极其相关:

强烈建议使用 dp 单位定义应用程序的 UI,作为确保 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:

At run time, the platform transparently handles any scaling of the dp units needed, based on the actual density of the screen in use.

(the platform does the scaling- the density ratio handling is done by Android; not you) and this is extremely relevant to the UI:

Using dp units to define your application’s UI is highly recommended, as a way of ensuring proper display of your UI on different screens.

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.

谜兔 2024-12-05 00:19:37

由于 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.

夜夜流光相皎洁 2024-12-05 00:19:37

在所有设备上都是相同的,因此它将根据在该公式中计算的像素数进行缩放。

The same on all devices so it will do scaling based on the number of pixels it computes in that formula.

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