How 9patchimage works? - android

I am working with android's 9 patch image emulator to create 9 patch image.
Can anyone tell me how it actually works?
On what basis it Scale the image?
What are the parameters it considers to stretch image?

Here are great paragraphs about 9 patch drawables https://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

It's basically a PNG image with 1px border around it. With the black pixels on the border you define an area witch is stretchable and area in which the content is allowed.
More information under the link provided by Konstatin.

Related

9 patch fill area not working with multiple scale areas

In my 9 patch I've added a fill area to be the contents taking most of the width and from below the red rectangle. However as the scale images show it's not working. If I remove the scale area shown by the arrow then it works perfectly.
What am I doing wrong or not understanding about 9 patch?
Thanks.
I found a related answer that explains it perfectly for me:
Android nine-patch content-border not honoured
The content region is defined by the right and lower bars as mentioned
above, but what you are really defining is the padding
(top,left,bottom and right padding).
So think of it in terms of "padding" around the content area, as defined by the white pixels.
Check out the linked answer for more detail.
I don't know what is your image,but I think it is better to exclude the red rectangle from image.
Then use a layout and put your background image, for its background,
and add Image element in that layout for displaying your red rectangle.

nine patch image doesn't work

I did a 9 patch image with android tool draw9patch.bat. Draw a black line only on the right side, where I want it to stretch. Saved as .9.png and set it to background of TextView:
android:background="#drawable/myninepatch"
It doesn't work, I get the image with the black line in the app and it doesn't stretch. What am I doing wrong?
I have seen at some places besides of the lines for the stretchable area there's also a very small marked area in the opposite side, but I don't know what is it for and I'm not using it.
Check this guideline . Here you can find simple UI Guideline for nine patch images .
In 9 patch images the right and bottom side lines indicate the padding, eg. the size which below the image won't shrink. The segment indicated by the top and left side line will stretch and shrink according to the desired size.

9-patch - 1px border is blurred

I'm testing on HTC Desire which is a hdpi device. The problem is clear when you look at the image:
And here is how my 9-patch (made with standard 9-patch tool) looks (zoomed):
The left image is my photoshop file, and the right one is a screenshot from my device. As you can see there is a huge difference. Let's say that the shadow is not THAT important, but the blurred border looks bad.
Is my 9-patch png wrong? What can I do to achieve a nice crisp 1px solid border?
You should use 9-path tool for each png in different drawable folder. Sometimes problem is this the Android try to convert the png and blur the black lines, what cause later problem with properly displaying.
To circumvent this problem simply drop the 9 patch inside the drawable-no-dpi folder.
This allows to have one image for all densities. And android won't touch the image.
This is mostly what I do for my projects, unless I really want to have a difference between the densities. In that case, I create one 9-patch per density.
Alternate solution:
Shrink your top and left markers by 1 pixel on each side. This will ensure that scalable part does not overlap the grey border.

Android - ImageView scaling

Is it possible to scale ImageView only in particular part?
I have a ImageView and I would like to scale it, but only the middle part.
Left side and right would stay the same, stretching occur only in the middle.
Just like on the pic bellow:
I personally did not know that by heart but simple search showed me this result: Android - stretch image partially
It sounds to me like you want to leverage 9 patch drawables. A 9 patch is just a special version of your image that has a 1px border that defines the stretchable areas. Android directly supports this, and even has a tool to create the 9 patch images. It's pretty simple.
Documentation here: http://developer.android.com/tools/help/draw9patch.html

Android What is wrong with this 9 patch?

Hello,
see above for my 9 patch image. It is created using the 9 patch editor from Android tools. I have multiple issues:
The nine patch tool in preview scales the image in height as well. Why? Zhere is no black line on the left.
The nine patch tool shows bad patches. I think that means that I still can use it but it might not look good. Is there any simpler shape than this? What goes wrong when stretching?
I cannot import this 9.png file into my project without errors. So technically there must be something wrong.
Thanks, A.
Seems like both top and left lines are required by 9-patch to determine the stretchable area. You could put one pixel there. You could later fix the height of the view which uses this image to prevent it from stretching in height.
You need to add stretch information for height too. A nine patch should be stretchable both horizontal and vertically.
The bad patches are warning of artifacts may be produced when stretching.
Try deleting stretch above the green/white transition of your image.
I think the missing vertical stretch is why you get error, assuming it is has the extension 9.png.
Tips for enhancing you nine patch:
By the looks of your image you have included the right border as stretchable too, you might want to remove that.
Consider only having 3 pixels stretch above the green content, then 1px above the white content to keep the aspect ratio. There is no need to stretch the whole image. You need only 1px transparent padding from image to stretch data.

Categories

Resources