Creating & Using 9-patch images in Android –
I’ve recently heard of pictures with nine spots. I know it’s nine tiles and it’s expandable. I’d like to know more about that.
How can I solve this problem?
The SDK and Android Studio come with the Draw 9 patch tool (Draw9patch in the SDK Tools folder), which is a simple editor. Here’s a nicer one, which is also open source. It has a simple but smart standard image.
The formal documentation has improved over the years. The main advantage of the nine patch images is therefore that they can specify (non-contiguous) areas for scaling:
The NinePatch graphic is a standard PNG image that contains an extra 1 pixel
. It must be saved with the 9.png extension in the
res/drawable/ directory in your project.
Use the limit to define the expandable and static areas of Figure
. You indicate an elongation by drawing one (or more)
1-pixel black line(s) at the left and top edges of the frame (the remaining
pixels of the frame must be completely transparent or white). You can have
as many expandable sections as you want. The relative size of the expandable parts
remains unchanged, so that the largest part always remains
You can also define an additional image segment
(actually lines) by dragging one line to the right and one line
down. If the visualization object defines the NinePatch graph as background and then specifies the view text, the view text is stretched so that all text occupies only the specified area on the right and the bottom line (if enabled). If intersecting lines are not enabled,
Android uses the left and top lines to define this rendered area.
But the doctors lack good examples. At the end of this tutorial you’ll find great examples that will answer the second part of your question and explain how scaling works – not just for buttons, but also for frames, as well as a complete sample project that you can download and play with.
Most examples relate to the creation of a 9 plane image, but implementation details generally remain at a high level.
Post Nick above – along with a good tutorial from Platform 9 that contains a file for uploading a saved working project tag.
Here are the basic implementation details that worked for me (once you have a 9-platform image ready) :
- Link the rendering to the name, but don’t take the .9.png with you (Eclipse autocompletion takes care of this).
- Make sure there is only one image under the root folder/sign folder (not the version for each dpi folder).
- The image should be set with :background, not :src (I was stuck on this for a while).
- Make sure you use the image and its layout:
How do I create a 9-page image? Is there some kind of tool? Can I make it from AndroidSDK or code?
When searching for the main search engine for the android 9-patch tool, the very first result is the android developers documentation page for the draw9patch tool.
Main advantages of the 9-patch compared to normal png ? (does it stretch dynamically/automatically depending on the screen) ?
The documentation for Android developers contains additional pages describing the nine PNG patch files. This documentation contains passages such as. B. :
The NinePatchDrawable graphic is an expandable bitmap that Android automatically adjusts to the content of the display you have placed it in as background. An example of how NinePatch is used is the background used by standard Android buttons – the buttons need to be stretched to fit lines of different lengths.
The 9-way picture allows you to select the part of the picture that can be stretched.
It should be an image in png format, and the name should end with .9.png (something like .9.png).
A simple tool for all densities:
Solution No 5:
What are NinePatch images?
NinePatch images are PNG images that mark the parts of the image to be stretched. You have an extension like image_name.9.png.
2. Where are they stored in the Android project?
How do I create a NinePatch image for your Android application?
The Android SDK contains the WYSIWIG draw9patch.jar tool in the folder /tools of your Android SDK.
Solution No 6:
Key advantages of the 9-pin connector over the conventionalconnector
9-patch images are scalable and repetitive images are reduced to their smallest size. The picture is not stretched or adjusted proportionally to different screen sizes. Another big advantage is memory.
The same small memory can be reused for devices with different screen sizes. Images of 9 well-designed platforms are less prone to errors and are highly reusable.
Solution No 7:
It’s a good tool:
9 Patch images are scalable and repetitive images that are reduced to a minimum size. The simplest example would be if you made a round dive and cut it into 9 squares as if it were a tic-tac-toe board. The four corners do not change in size at all, but remain static, while the other five patterns are stretched or repeated so that the entire image can be scaled accordingly.
With this statement and the advent of CSS3 you might think that there is no reason to use 9 Patches, but the name 9 Patches is a misnomer. Images can be split up into even smaller pieces.
9 Patch images contain a hint about the fragment in question by adding a 1px border to the image. The colours on the border determine whether the border is static (without scale), stretched or repetitive.
Google slide show: https://docs.google.com/present/view?id=dc7ghz8w_34f8338rcg
See also the information for Android developers on images with 9 patches: http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch.
Solution No 8:
Step 1, Since you are not yet known, first prepare an image in png xxxhdpi format.
Step 2 : The desired image field should be as short as possible (remove the superfluous color portion), because the 9-pin section does not reduce the image, but enlarges it.
For example, I personally use the ImageMagick command line tool to convert:
convert -trim ‘my_image.png’ png32:my_image_trim.png #trim extra transparent surrounding image
rm lala* ; convert my_image_trim.png -crop 310 +repage +adjoin png32:lala%02d.png #cut redundant/repeated middle part
convert +append lala02.png lala05.png png32:out_right.png #append left/right adjacent images
convert -resize 144×144! out_right.png png32:my_image.png #resize to desired dpi format
Conversion is a trap: I have to prefix it with png32 otherwise I get a black striped picture 9, see this wire.
Step 3, copy the image to Android Studio with the Render option, then right click and choose Create 9-Patch File….. in the menu. A new .9.png image with the same name will be created. You can now simply delete the original image. Be careful when playing with the refactor to rename it for backup, because this will also rename the XML image ID and make you wonder why the 9 platform image doesn’t work, because the XML always points to a non-9 platform image.
Step 4, The 2 black lines on the left and above form an expandable rectangular area, while the 2 black lines on the right and below form a rectangular text area.
The size of the image increases relative to the advertised expandable area as the text gets larger. A text field, on the other hand, means that text is only allowed in this field.
You don’t have to draw the black dots/lines out of nowhere, the 4 black lines are already present in this image behind the top, left, bottom and right edges when using the 9-pattern image generated by Android Studio. Zoom in if you can’t see how to draw these lines.
The starting position of the 2 black lines, vertical and horizontal, both explain an extendable zone:
- The original position of the left vertical line is between the left upper and left lower edge.
- The starting position of the upper horizontal line lies between the top left and the top right line.
The initial position of the 2 black lines, vertical and horizontal, both explain the text box :
- The starting position of the vertical straight line is between top right and bottom right.
- The starting position of the lower horizontal line is
between bottom left and bottom right.
Above you can see the starting position of the black lines before you start dragging to reduce the length and to adjust the start and end position of these lines.
Expandable fields and text fields may vary as needed. But in general, the expandable area should be equal to or smaller than the text area, a classic example would be an image of a chat bubble:
In the image above, the width of the upper and lower black lines is equal, and the right black line is higher than the left black line, which also means that the text goes through to half of the lower curve of the minimum size and the enlarged size. And it only stretches in the text box.
Discover now the two advantages of the 9-Patch : The bottom line and the right line together form a text area that perfectly guarantees that the text never goes beyond the curve of the image! And also define which part of the text area is responsible for expanding the image as ascending text when saving the curve without scaling.
Move the mouse pointer over the line to see the x,y positions in pixels, this allows you to measure whether the two lines are equal or smaller in position.
Pay attention to the position of the line when you drag it, because the left line can be moved to the right and the right line can be moved to the left and you lose the overview of which line is expandable and which text.
There are two important checkboxes to tick, the Show patches and Show bad patches checkboxes.
If you draw an expandable area in the Show Bad Patches box, but instead of a straight line, a curved line is placed over it in your image, this area will be marked in red as a warning. You can shorten your line to divert this red warning, or just ignore it. Note that a red warning can be misleading, which can in fact be caused by the opposite line, in which case you should reduce the opposite line to avoid the red warning.
Step 5, in the xml, you can refer this image to TextView background as shown below, use wrap_content to make it expandable:
There is no filling in the text display or a nice layout for the parents the first time, or you might wonder why it doesn’t work as expected.
An example of an important context:
creating synonym,created synonym,making synonym,making,define make,define develop,creating meaning in hindi,create,define generate,creating creating,define fabricate,creating a website,define produce,creating meaning in life