Why do we need 9-patch?

痴心易碎 提交于 2019-12-01 06:03:50

问题


My question is like title says: Why do we need 9-patch images? Are they really so important? Actually, what they really do? I don't get concept of 9-patch images clearly.

I know that 9-patch images are scalable. So let's say I have some image button which perfectly fits on my ldpi device. With Draw 9-patch tool (part of android SDK) I can mark scalable areas and this image will nicely fit on hdpi and even xhdpi devices. Edges will be nice and smooth.

But is this really good pratice or is just an alternative? Should i create every image for ldpi, mdpi, hdpi and xhdpi devices or can I just use Android SDK "Draw 9-patch" on one image and let Android handle various devices?

I'm not that concernet about edges, but the actual content of the picture. Is content scalable too?


回答1:


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




回答2:


The logic behind 9 patch is that you do not scale the corners.




回答3:


In android 9-patch is used when you want parts of your image to scale and other parts to keep their original dimensions. the first application comes to mind is the corners as you stated. So you use them in that case.

On the other hand if you want your resources to appear in good quality (without blurring) you should use alternative resources (ldpi, hdpi ..)

Take a look here :Screen Support




回答4:


Nine-patch images are scalable but only in some areas. The simplest example is a box divided in 9 parts: corners won't be scaled; borders will extended in one direction according to the content in the middle.

The concept exists in other languages. iOS, CSS3, W3C box model are such examples. Yet 9-patch images are easy and flexible to use because:

  1. several areas can be extended instead of one for other languages;
  2. scalable areas are defined in the image and not in the code.


来源:https://stackoverflow.com/questions/16521594/why-do-we-need-9-patch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!