Search code examples
androidnine-patch

9patch is stretching areas I didn't mark


I've created a 9patch for a background area and it seems to be stretching areas I didn't mark to be stretched. I have it so that vertically it should stretch 1 pixel from the middle, but it's still stretching the top and bottom borders. Here's my 9patch, placed in the drawable folder:

9patch

And here's the resulting image when placed as the background attribute for a layout:

screenshot

Why is it stretching the top and bottom borders? I want these to stay 2 pixels on the top and 4 pixels on the bottom, but its stretching.

You can download the image here if you want to try this out. Rename it so it has a .9.png extension, place it as the background in a View about 100dp tall, take a screenshot, then zoom in and you can see what I'm seeing.


Solution

  • It's being scaled. If you place it in res/drawable, it's going to scale up 1.5x on HDPI devices, and 2x on XHDPI devices (drawables there are assumed to be MDPI). You can either place it in res/drawable-nodpi to keep it from scaling, or make larger versions if desired and place them in res/drawable-hdpi/res/drawable-xhdpi folders.