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
Related
I am learning about Android UI and am unclear why people use nine-patch, when you could use vector graphics, because those are scalable without any pixel degradation. I am a beginner in Android, so I hope I am not missing anything here, but it seems like it would be easier to build vector graphics and use those. You would not need the special editor to build them.
Can anyone explain the advantages to using nine-patch over vector? (Don't just explain advantages of nine-patch, as that is already done on StackO., but rather the advantages that vector does not have). Because it seems like Android recommends nine-patch. Thanks.
In vector graphics all side are scale or stretch when we set it to any background whereas in 9-patch we can define which sides can scale or strech so at runtime only those side scale which we set it to scale in 9-patch tool.
from this
-> The advantage of using 9-patch images is that when using it as a background, for instance, the image won't stretch and loose proportions in different screen sizes. the center 'patch' will remain as is and the 'borders' patches will be stretched to fit the screen/view size.
let say you have this image.
and a button with fill parent width. if you set this image to button background it will scale completely and your image gets blur (mean t will expand to button width)and it will not look good. so what 9 - path tool do that you define that online scale some part of image let say if width is fill parent. dont scale whole image . let say we set that after t (in image). scale whole area, so t will not get blur. so this will make good your button.hope you got my point..:)
after making your image 9 patch and setting to button background. your button look like this.
instead of t(in image). whole area expand and fill the buttons width.
Imagine a button with rounded corners. How do you scale it? If you scale it only horizontally, you will have elliptic corners, which would be ugly.
This is what 9-patch is for.
i hate 9patches. i am quite attentive when doing my artwork. i don't even use photoshop. i go with illustrator.
i do everything right when exporting my artwork, i used to do my 9 patches with insane zoom on, maybe check afther that in photoshop for misplaced pixels...
** sad trumpet ** when put on a view, if i used some subtle round corners like 6px or 10px and a stroke everything looks awful at mdpi.
So I solved my problems by marrying the two. I wrote my own 9patch which uses vectors. :)
Everything looks like it's been touched by baby Jesus. Perfect corners, strokes and, best of all, you can use one asset for all the screen sizes, densities while, of course, no more transparent borders, wicked errors because 9patch won't stretch inward, so on and so forth.
I use vectors for icons too. While there sometimes are issues with various effects, these are minimal and easily avoided if you do some reading on how to avoid them.
Best of luck to all you guys!
This is a very basic example.
You can do whatever strikes your fancy. Because of the performance impact of svg's on an app, when first run [or when the user changes appearance options, i like to save the newly generated bitmaps as pngs, if possible.
You don't get any more "best of both worlds" than this.
You can define content are on a 9-patch image which means (for example) text will always be placed in that specific area, I think you cannot do this in Vector image. Android has built-in support for 9-path but for vectors you have to use a library.
9-patch rendering is easy to implement and efficient. If you have an image that can be scaled by stretching horizontal or vertical lines (e.g. buttons or rectangular icons), then use a 9-patch. If you have some icons that don't scale well, then create multiple versions at different resolutions and use Android's resource management to handle it. Both of these approaches are much easier and more efficient than vector graphics.
If you have large images or scenes to render, and you don't want to take up a ton of space with bitmaps, thats when you start thinking about vector graphics.
I'm trying to make a 9 patch image to be a header of my application but it doesn't work.
This is the image:
As you can already imagine, I want only the middle (blank) part to be streched and nothing else. It works normally in eclipse screen preview but it stretches it as if it wasn't 9 patch when i run it on my device.
I've had problems like this before so I'm clearly doing something wrong with 9 patches, I looked up a lot of info and tutorials on 9 patches and I just don't seem to get it. Could someone give me a good explanation where exactly to put the black dots for it to work and why?
When you put those extra lines in an image, they work like this:
Top and Left extra lines define the vertical and horizontal pixels which will be stretchable (the intersection is called stretchable area).
Bottom and Right extra lines are optional and define the padding. In this case, the intersection is the region in which the content will be placed (the rest is the padding).
I recommend you to use the 9 Patch editor included with the Android sdk. It is called draw9patch and can be found at <android-sdk-root>/tools. You will be able to see a preview of your image with 9patch.
Links:
Draw 9-patch
NinePatch
2D Graphics - Nine Patch
I can't believe it, the next thing I tried after asking a question worked. The fact that I had TWO lines in the bottom was the problem. I just put a single line BETWEEN the images in the bottom and it worked.
Like this:
If anyone still gives me a good explanation for each of the four sides of the image (why and where should the dots be put), I will still accept that answer.
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.
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.
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.