I recently heard about 9-patch images. I know its 9 tiled and is stretchable. I'd like to know more about it.
Active10 months ago
How can I create a 9-patch image? Is there any tool? Creating & Using 9-patch images in Android. And it has a complete example project that you can download.
user8389458
Nizam
Draw 9-patch Tool DownloadNizam
4,41588 gold badges3434 silver badges5353 bronze badges
8 Answers
Today I discovered nine patch images. The official documentation (linked to in the other answers) is ok I suppose, but it really lacks in examples.
This little tutorial has some great examples at the end that answer the second part of your question, explaining how the scaling works - not just for buttons - but also frames, and it has a complete example project that you can download and play with. The most important advantage over PNG is that nine patch images can specify non-contiguous areas to scale.
To answer the first part of your question, the SDK (and now Android Studio) ships with 'Draw 9-patch' ('draw9patch' in the SDK tools folder) which is a simple editor. Here is a nicer one which is also open source. It has a simple but clever default image.
Nick WestgateNick Westgate
2,12211 gold badge2222 silver badges2929 bronze badges
Most of the examples talk about creating a 9-patch image, but implementation details are usually left at a high level.
Nick's post above - with the good 9-patch tutorial that provides a working project download file , saved the day.
Here are the main implementation details that worked for me (once you have a 9-patch image ready to go):
Optimized for Immersive Gaming Action through Precision Audio The Xonar DG sound card combines Dolby Headphone 5.1 high definition surround with ASUS’ own GX 2.5 gaming audio engine and an on-board headset amplifier. Asus audio card driver download. Download and Update ASUS Audio Cards Drivers for your Windows XP, Vista, 7, 8 and Windows 10. Here you can download ASUS Audio Cards Drivers free and easy, just update your drivers now. ASUS Sound / Audio Free Driver Download| Keep your ASUS Sound / Audio drivers up to date with the world's most popular driver download site. Hear All, Dominate All! Optimized for immersive gaming action through precision audio The Xonar DGX audio card combines Dolby® Headphone 5.1 high definition surround with the ASUS-exclusive GX 2.5 gaming audio engine and an on-board headphone amplifier.
Community♦
Gene BoGene Bo
6,20744 gold badges4747 silver badges9797 bronze badges
How can I create a 9-patch image? Is there any tool? Can I create it from AndroidSDK or code?
When you search a major search engine for
android 9-patch tool , the very first hit is for the Android developer documentation page on the draw9patch tool.
Main Advantages of 9-patch over regular png? (is it stretchable dynamically/ automatically according to screen?)
The Android developer documentation contains other pages that describe nine-patch PNG files. This documentation includes passages like:
A NinePatchDrawable graphic is a stretchable bitmap image, which Android will automatically resize to accommodate the contents of the View in which you have placed it as the background. An example use of a NinePatch is the backgrounds used by standard Android buttons — buttons must stretch to accommodate strings of various lengths.
CommonsWareCommonsWare
812k145145 gold badges19631963 silver badges20072007 bronze badges
With 9-patch image you can choose which part of your image may be stretched.It must be png image and name must end with .9.png (something.9.png)
Simple tool for all densities:
Petr KrejčíPetr Krejčí
1.What are NinePatch images?
NinePatch images are PNG images that mark the parts of an image that can be stretched. They have an extension like
image_name.9.png .
2.Where they are stored in android project ?
3.How to create NinePatch image for your anndroid app ?
Android SDK includes a WYSIWIG Tom11
draw9patch.jar tool inside your Android SDK /tools folder.
1,45344 gold badges2222 silver badges4343 bronze badges
Prateek JoshiPrateek Joshi
Healing Brush
2,51211 gold badge2727 silver badges3939 bronze badges
Main Advantages of 9-patch over regular png
Actually 9 Patch images are stretchable, repeatable images reduced to their smallest size.the image won't stretch and loose proportions in different screen sizes. One more and biggest advantage is memory.
Same small size memory can be reused for different screen size devices. Well-designed 9-patch images are less error-prone and have high re-usability .
Kaushik
5,48255 gold badges3030 silver badges4949 bronze badges
IntelliJ AmiyaIntelliJ Amiya
57.7k1313 gold badges127127 silver badges145145 bronze badges
This is good tool:Click here.
9 Patch images are stretchable, repeatable images reduced to their smallest size. The simplest example would be if you were to take a rounded div and slice it up into 9 squares like you would a tic-tac-toe board. The four corners wouldn't change sizes at all but would be static while the other 5 pieces would be stretched or repeated to allow the whole image to scale appropriately.
With that explanation and the advent of CSS3 you might think that there is no reason to use 9 patch images but the name '9 patch' is a misnomer. The images can be sliced up into even smaller pieces.
9 Patch images contain an index of which piece is what by adding a 1px border to the image. The colors in the border determine if a piece is static (doesn't scale), it stretches, or it repeats.
Google Slideshow: https://docs.google.com/present/view?id=dc7ghz8w_34f8338rcg
See also the Android developer info about 9-patch images: http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
Roga MenRoga Men
Step 1, Since you are not familiar yet, prepare a xxxhdpi png big image to play around first.
Step 2, That image desired expandable field must shorter (by removing redundant/repeated color part) as possible since 9-patch no such thing 'reduce', but 'expand' the image.
I personally use the ImageMagick command line tool to convert it, e.g.:
There is one pitfall when convert: I must prefix with png32, or else will get black 9-patch image, see this thread.
Step 3, Copy image to Android Studio drawable, then right-click and choose menu item 'Create 9-Patch file..'. A .9.png new image will be generated with the same image name. Now can just delete the original image. Careful when playing around with refactor to rename for backup since it will rename the XML image id too, and make you wonder why the 9-patch image not working since XML still referring non-9-patch image.
Step 4, Left and top 2 black lines form an expandable rectangular area, while right and bottom 2 black lines form a text rectangular area.
Your image size will grow on your declared expandable area as text growing. While the text area means text only allow in that area.
9 Patch Tool
You don't have to draw the black dot/lines from scratch for simple usage, 4 black lines already existed behind the top, left, bottom, right on that image if your 9-Patch image generated by Android Studio. 'Zoom in' to bigger if you can't see to drag that lines.
The original position of 2 black lines, vertical and horizontal, both declare an expandable area:
The original position of 2 black lines, vertical and horizontal, both declare a text area:
Above is the original position of black lines before you start to drag to narrow down the length, to adjust the start position and end position of that lines.
Both expandable and text areas can be different depends on your needs. But normally expandable area should equal OR less than text area, a classic example will be chat bubble image:
The image above has equal top and bottom black lines width but right black line is higher than left line, which also means that text consistently stay up to half of bottom curve in either minimum size or expanded size. And it only expand in body of text area.
Now you will know the two benefits of 9-Patch: the bottom and right line together form a text area which perfectly ensures text never overflow to outside of image curve ! And also defined which portion of text area responsible to expand the image as text growing, while keep the curve without scaling.
Hover the line will able to see the x,y positions in pixels, it help to measures both lines are equal or less in position.
You should remember the position of line when dragging, since left line can drag to right side, and the right line can drag to left side, and you may get lost of which line is expandable line and which line is text line.
There are 2 important checkboxes you should tick, i.e. 'Show patches' and 'Show bad patches' checkboxes.
In 'Show bad patches' checkbox, if you drawing the expandable area but covering the curved line of your image instead of a straight line, then it will mark that area as red to warn you. You can narrow your line to dismiss that red warning, or just ignore it. Keep in mind that, the red warning may misleading which may actually be caused by the opposite line, in that case, you need to narrow the opposite line to dismiss that red warning.
Step 5, In xml, you can refer to that image as TextView background like below, use
wrap_content to make it expandable:
When first time playing around with it, ensures no padding on textview or fancy parent layout, or else you may wonder why not working as expected.
Button background example:
林果皞林果皞
4,81322 gold badges2929 silver badges4949 bronze badges
Not the answer you're looking for? Browse other questions tagged androidandroid-drawablenine-patch or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2020
Categories |