Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different 9-patch images for different densities?

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

like image 904
user949884 Avatar asked Mar 30 '12 10:03

user949884


2 Answers

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.

like image 170
Janusz Avatar answered Sep 22 '22 21:09

Janusz


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

like image 38
Shubhayu Avatar answered Sep 23 '22 21:09

Shubhayu