Different 9-patch images for different densities?
Asked Answered
S

2

27

I have the same 9-patch images in MDPI / HDPI / xhdpi. Is there a particular reason why it will not be saved for the smallest size? The stretchable areas scale automatically when the content is greater, for example a background as a 9-patch.

Example background:

mdpi: mdpi

hdpi: hdpi

xhdpi: xhdpi

Shelashelagh answered 30/3, 2012 at 10:44 Comment(0)
F
36

Many nine patches are so small that they normally don't need to be recreated for different screen sizes.

In your example the corners of the background are not scaled. If you just use the same corners on all resolution they would have a different size on different resolutions. On smaller phones the corners would appear bigger on bigger phones the corners would appear smaller.

Another example would be a line. If you have a line that is has a special effect and you create it as a nine patch for mdpi screens and the line is two pixel high. Just reusing this drawable without scaling on a xhdpi screen would make your line two pixel high on this screen. But because the screen has smaller pixels (higher resolution, same display size) the line would appear thiner.

Therefore you always need to create a 9-Patch for every resolution, or as @Shubhayu said only produce high resolution nine patches and accept that they get blurry if the system scales them down for other resolutions.

Floyfloyd answered 30/3, 2012 at 11:13 Comment(3)
Understand now. Greetings to Bielefeld :-)Shelashelagh
Yes you are right about the 2 pixel high example. Though I hadn't observed a problem with images being scaled down. It is possible that I didn't deal with images that would show the difference. Thanks for the tip :) I shall keep that in mind next time around.Stereotropism
And if your lazy, you can try this tool : code.google.com/p/9patch-resizer I just uploaded it, it generates ldpi, mdpi and hdpi version of any png, or nine patch pngHeine
S
7

You don't need to put different sized 9-patch images in all the 3 folders. Just put the one with the highest resolution in your drawable folder. Android will pick it up for all your different densities.

Also the crispness of your image depends upon the resolution of the image. You'll see blurry stretching occur for high density devices if you save the smallest size (ie with the least resolution) This will not be evident if the background image is plain colored. You'll notice the difference when the background has some designs/repeatable of gradients.

Edit: Ever since writing this post, I have discovered a lot more stuff about the 9-patch. 9-patch images do not scale down. Best would be to put the smallest size image. And based upon how it looks for other densities, you should make 9-patch images for the affected densities. Here are more details on it,

http://www.shubhayu.com/android/9-patch-image-designers-vs-developers

Stereotropism answered 30/3, 2012 at 11:0 Comment(4)
And it will resize the images from the highest density to match the chosen density and from the resized image on it will use the nine patch mechanism to stretch the images. The will result in blurry images and is not recommended.Floyfloyd
9-patch scale down? I thinks only scale up.Shelashelagh
If Android picks a drawable from an xhdpi folder for an mdpi phone it will scale down the image regardless if they are 9-Patch or not. See my answer for more information. I also had a hard time learning this ;)Floyfloyd
This may not be true for real devices, but the Android emulator does not handle 9-Patch assets the same as other drawables. Emulators with API 8-10, require baseline 9-Patch assets or it will crash with a ResourceNotFoundException. Other assets do not have this problem. On API 17 the emulator does not crash but the 9-patch assets don't stretch as expected, often rendering entirely black. It seems as though 9-Patch assets have to be created for all supported densities, just to ensure your app doesn't crash or look ridiculous.Harborage

© 2022 - 2024 — McMap. All rights reserved.