How to use 9 patch images in android for different-2 densities(like mdpi,ldpi,hdpi).?
Asked Answered
E

3

5

I am creating an android application in which I want multiple screen support.For these I have used 9 patch images.

But my question is this whether using 9 patch images will be enough for different-2 density's devices or I will need to create different-2 9 patch images for varying densities(like mdpi,ldpi,hdpi).

Thanks in advance

Esperance answered 28/9, 2011 at 12:42 Comment(1)
Android How to use 9 patch pngGingerich
G
17

I recently found out myself in the same situation so let me pitch in and expand on what has been said already...

Yes, 9-patch images will scale, that's what they exist for. But if you should use a 9-patch image for all screen densities, that depends on the image really. 9-patch images are more commonly used, for instance, buttons. You can have different sized buttons in your app and a 9-patch image will help deal with them, no matter how you size your button (as long as the 9-patch image is properly created).

But let's say your button design has some really round corners for the hdpi version. You create your 9-patch image without messing the corners but when you look at it in the ldpi version, you'll realize your corners are too big for that low resolution. For this situation, you'll need a different 9-patch image with less round corners, that look better on that resolution.

So, the final answer is, it really depends on your image. If you can create a 9-patch image that looks good in all densities, than fine, use it, as you only need one image to handle all densities. But if it doesn't look good, because of corners, gradients, or whatever, than you'll need one 9-patch image for each screen density.

Hopefully it's clearer now.

Gadson answered 4/10, 2011 at 18:8 Comment(5)
if the image is a normal png and not 9patch png, will the image still be scaled or is the 9patch an actual hard requirement for any scale functionality?Solangesolano
@Solangesolano Image will be scaled if your view has size more than the image's size.But 9 patch image is used for saving from riping.Esperance
@AB1209, can you tell me what you mean by "saving from riping"?Solangesolano
@Solangesolano I mean 9 patch image save image to being distorted when used on any view.Esperance
It depends on the padding as well. I've seen 9-patches completely break because the resampling between densities smudges out the padding lines. This may depend on device / Android version.Fridge
E
2

From documentation: nine patch

A NinePatchDrawable graphic is a stretchable bitmap image, which Android will automatically resize to accommodate the contents of the View in which you have placed it as the background. An example use of a NinePatch is the backgrounds used by standard Android buttons — buttons must stretch to accommodate strings of various lengths. A NinePatch drawable is a standard PNG image that includes an extra 1-pixel-wide border. It must be saved with the extension .9.png, and saved into the res/drawable/ directory of your project.

the answer is no. you nine patch will scale between different screen size

Enthusiasm answered 28/9, 2011 at 12:51 Comment(4)
That is not entirely correct. Yes the ninepatch will scale but you should still provide ldpi/mdpi/hdpi images because the parts that don't scale should have the correct size too.Sarsen
But what about different-2 densities ? Shall I need to create high and low density's 9 page images also ?Esperance
@AB1209 You don't HAVE to but you can if you were to find that your NinePatch parts don't scale well.Roentgenotherapy
Boils down to look&feel. I'm usually satisfied with one 9 patch for all sizes and densities.Dianetics
M
0

Short answer is YES. Check this:

By default, Android scales your bitmap drawables (.png, .jpg, and .gif files) and Nine-Patch drawables (.9.png files) so that they render at the appropriate physical size on each device. For example, if your application provides bitmap drawables only for the baseline, medium screen density (mdpi), then the system scales them up when on a high-density screen, and scales them down when on a low-density screen. This scaling can cause artifacts in the bitmaps. To ensure your bitmaps look their best, you should include alternative versions at different resolutions for different screen densities.

from Android Developer Official Doc

Mullite answered 1/6, 2016 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.