3

我的问题就像标题所说:为什么我们需要 9 补丁图像?它们真的那么重要吗?事实上,他们到底在做什么?我不清楚 9-patch 图像的概念。

我知道 9-patch 图像是可扩展的。因此,假设我有一些非常适合我的 ldpi 设备的图像按钮。使用 Draw 9-patch 工具(android SDK 的一部分),我可以标记可扩展区域,并且此图像非常适合 hdpi 甚至 xhdpi 设备。边缘会很好而且很光滑。

但这真的是很好的做法还是只是一种选择?我应该为 ldpi、mdpi、hdpi 和 xhdpi 设备创建每个图像,还是可以只在一个图像上使用 Android SDK“Draw 9-patch”并让 Android 处理各种设备?

我并不关心边缘,而是图片的实际内容。内容也可扩展吗?

4

4 回答 4

7

The content is scalable, the edges will not be scaled, in typical usage.

They're generally used for backgrounds to buttons, or other screen decorations, where the scaled content can be scaled infinitely without losing resolution.

Ideally, they're used in addition to density-dependent resources where appropriate, so that the section of the resource (which you didn't explicitly state is scalable) isn't upscaled and subsequently pixelated.

Consider the splash screen for the Kindle app on Android, which features a silhouette of that boy reading against a tree. In a simplified version, the ground can be scaled horizontally infinitely, but if the boy was scaled, he'd appear skewed. So you could use a nine-patch and specify the section which can be scaled, and in which direction.

That's not enough though - if you only included a low resolution resource, the resource would still be scaled up initially if the device display was of a higher density. In this instance, the boy might look blurred from the upscaling, and is an example of when a higher resolution resource could have helped prevent the pixelation.

The way you've phrased the question suggests (to me) that you're asking whether 9-patch images can replace all drawable resources, like icons or resources with pictures in them, to which the answer is no. They're only to be used to scale sections of resources which feature a block of a single colour (i.e. sections which cannot be pixelated).

--

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

于 2013-05-13T12:08:56.367 回答
1

在 android 9-patch 中,当您希望图像的某些部分缩放而其他部分保持其原始尺寸时使用。想到的第一个应用程序是您所说的角落。所以你在这种情况下使用它们。

另一方面,如果您希望您的资源以良好的质量出现(不模糊),您应该使用替代资源(ldpi、hdpi ..)

看这里:屏幕支持

于 2013-05-13T12:10:15.103 回答
1

9 补丁背后的逻辑是你不缩放角落。

于 2013-05-13T12:10:51.750 回答
0

九个补丁图像是可扩展的,但仅限于某些区域。最简单的例子是一个被分成 9 个部分的盒子:角落不会被缩放;边框将根据中间的内容向一个方向延伸。

这个概念存在于其他语言中。iOS、CSS3、W3C 盒子模型就是这样的例子。然而 9-patch 图像使用起来既简单又灵活,因为:

  1. 可以扩展几个区域,而不是扩展其他语言的一个区域;
  2. 可缩放区域是在图像中定义的,而不是在代码中。
于 2013-05-13T12:20:49.907 回答