经常用,在此做一记录:
简言之,做9patch图片的工具在tools下的draw9patch工具,然后左上画黑线的地方分别表示扩展的区域,拉伸的话会拉伸黑线部分,非黑线部分不做扩展
右下表示图片的显示区域,只显示黑线区域的内容,非黑线区域不显示,具体描述和图例如下:
Supporting different screen sizes usually means that your image resourcesmust also be capable of adapting to different sizes. For example, a buttonbackground must fit whichever button shape it is applied to.
If you use simple images on components that can change size, you willquickly notice that the results are somewhat less than impressive, since theruntime will stretch or shrink your images uniformly. The solution is using nine-patch bitmaps,which are speciallyformatted PNG files that indicate which areas can and cannot be stretched.
Therefore, when designing bitmaps that will be used on components withvariable size, always use nine-patches. To convert a bitmap into a nine-patch,you can start with a regular image (figure 4, shown with in 4x zoom for clarity).
![](http://android.toolib.net/images/training/button.png)
Figure 4. button.png
And then run it through the draw9patch
utility of theSDK (which is located in the tools/
directory), in which you can mark the areas thatshould be stretched by drawing pixels along the left and top borders. You can also mark the areathat should hold the content by drawing pixels along the right and bottom borders, resulting infigure 5.
![](http://android.toolib.net/images/training/button_with_marks.png)
Figure 5. button.9.png
Notice the black pixels along the borders. The ones on the top and leftborders indicate the places where the image can be stretched, and the ones onthe right and bottom borders indicate where the content should beplaced.
Also, notice the .9.png
extension. You must use this extension, since this is how the framework detects that this is a nine-patchimage, as opposed to a regular PNG image.
When you apply this background to a component (by settingandroid:background="@drawable/button"
), the framework stretchesthe image correctly to accommodate the size of the button, as shown in various sizes in figure6.
![](http://android.toolib.net/images/training/buttons_stretched.png)
Figure 6. A button using the button.9.png
nine-patch in various sizes.