본문 바로가기
생활일기/trend

Nine-Patch Stretchable Image

by 신일석 2009. 2. 8.
링크 : http://code.google.com/intl/ko-KR/android/reference/available-resources.html



Android supports a stretchable bitmap image, called a NinePatch graphic. This is a PNG image in which you define stretchable sections that Android will resize to fit the object at display time to accommodate variable sized sections, such as text strings. You typically assign this resource to the View's background. An example use of a stretchable image is the button backgrounds that Android uses; buttons must stretch to accommodate strings of various lengths.



A NinePatch drawing is a standard PNG image that includes a 1 pixel wide border. This border is used to define the stretchable and static areas of the screen. You indicate a stretchable section by drawing one or more 1 pixel wide black lines in the left or top part of this border. You can have as many stretchable sections as you want. The relative size of the stretchable sections stays the same, so the largest sections always remain the largest.



You can also define an optional drawable section of the image (effectively, the padding lines) by drawing a line on the right and bottom lines. If you do not draw these lines, the first top and left lines will be used.



If a View object sets this graphic as a background and then specifies the View object's text, it will stretch itself so that all the text fits inside the area designated by the right and bottom lines (if included). If the padding lines are not included, Android uses the left and top lines to define the writeable area.



The Draw 9-patch tool offers an extremely handy way to create your NinePatch images, using a WYSIWYG graphics editor.



Here is a sample NinePatch file used to define a button.






This ninepatch uses one single stretchable area, and it also defines a drawable area.


Source file format: PNG — one resource per file


Resource source file location: res/drawable/some_name.9.png (must end in .9.png)


Compiled resource datatype: Resource pointer to a NinePatchDrawable.


Resource reference name:

  • Java R.drawable.some_file

  • XML @[package:]drawable.some_file