Different 9-patch images for different densities?

user949884 picture user949884 · Mar 30, 2012 · Viewed 9.1k times · Source

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

Answer

Janusz picture Janusz · Mar 30, 2012

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.