Android Draw 9-patch tool: What is the meaning of black lines outside the figure
Asked Answered
G

1

14

I tried enough to find in google & android this question. Also did several trials & errors, but couldn't get this question. I was learning Android's "Draw Nine Patch" images. One basic I know is that, whatever image you process must be in .png format (if it's already in .9.png then it will be ignored by tool). However,

  1. I couldn't understand what it literally means by "stretchable patches" ?
  2. What exactly happens when you draw black dots outside the .png image ? (preview pane shows changes happening, but I am unclear on how it makes those)
  3. When I draw black dots in left/top sides of image it shows some changes in preview; but why isn't there any effect when you do the same on right/bottom side of the image ?
  4. Why this tool is used primarily for background, when we try to process on the image itself ?
  5. I am trying to modify a simple button.png (given in android sdk). Whenever I use this tool & draw black lines, it reduces the size of the image instead of 'stretching' it! Why ?
  6. What is the significance of options given below like, "show lock", "show content", "Patch scale" and so on ?

I apologize for asking so many questions, but in Android online docs they haven't explained well for novice. If someone can answer these, it will become ready reference for all the begineers who search this forum.

Gilder answered 20/11, 2010 at 5:29 Comment(1)
+1 for asking what the "lock" is about. I found it a little confusing at first, but basically it's just the editor's way of only allowing you to change the border lines of 1 pixel each and not anything else. So you can show it or hide it and it doesn't matter.Outdate
K
26

By stretchable patches, you're basically telling Android which rows and columns of pixels in the image that you want to repeat. When you stretch a standard image, there are two possibilities: One, it is scaled proportionally, but still loses sharpness due to interpolation; Two, it is scaled disproportionately, and loses not only its sharpness, but its shape as well. An example of a disproportionate scaling is below:
stretched

So the purpose of the black lines it tell Android what areas of the image are safe to repeat. The top corner defines the column(s) that it can stretch, while the left corner defines the row(s) that can stretch. The bottom and right corners just define the actual content area (e.g. where the button is allowed to place text), you can reserve extra space to pad the frame. In the image below, you can see that the two black pixels on the outside of the frame define rows on the image, while the one on top defines a column.


example

And below, this shows the result of a 9-patched stretched out to various sizes. If it's enlarged to be wider, the halves of the image on the left and right of the defined stretchable column are aligned to the left and right of the new size, and the defined column is repeated to fill the space in between. Same thing happens with the defined rows; if you use multiples (I don't believe you can use more than 2 stretch rows/columns) it just evenly pads the space with both of them; in this case I used it to keep the gradient evenly split down the middle.

stretched images result

Kep answered 20/11, 2010 at 6:39 Comment(2)
Hi Thanks for your explanation; but still I am not clear because trying to draw few 9 patch images, I am not able to stretch my button.png at the fullest. Also, I have several other questions too as mentioned.Gilder
I really don't know what you mean by Question 4. As far as 5 goes, drawing the black lines doesn't change the size of your image at all. Consider your image to be the absolute smallest size of whatever you'll be using it for. It's designed to be able to EXPAND to any size without distortion. "Show Content" shows what the defined content area is, e.g. where it can place content within. "Show Lock" I'm not familiar with. "Patch Scale" just lets you see the 9-patch scaled to various sizes.Kep

© 2022 - 2024 — McMap. All rights reserved.