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

前端 未结 3 1987
情话喂你
情话喂你 2021-02-10 11:04

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 w

相关标签:
3条回答
  • 2021-02-10 11:45

    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

    0 讨论(0)
  • 2021-02-10 11:55

    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

    0 讨论(0)
  • 2021-02-10 11:56

    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.

    0 讨论(0)
提交回复
热议问题