9 patch image is scaled at the wrong positions
Asked Answered
I

2

5

I am using a nine patch image as a drawable for a selector's item element. The nine patch image is here:

9 patch image

The image that finally gets rendered is this (emulator & device screenshot, both device have resolutions > mdpi):

resulting rendered image

As you can see, it has been scaled outside the stretchable area (as defined in the 9 patch file), the darker green bar is much thicker as it should be. Why is that and can it be avoided? The draw9patch tool scales this image correctly. Does it have to do with Android trying to preserve physical dimensions on high resolution devices?

Impetus answered 7/2, 2013 at 15:13 Comment(1)
Let us see the code where you draw the image.Lueck
C
6

Do you only provide a mdpi version of the image?

Android scales all images that aren't available in the correct pixel density. This happens before the image is scaled via 9patch.

This would mean that your image gets scaled up to fit hdpi and then scaled to fit the content.

To fix this, you have to provide hdpi and xhdpi versions of your image. 9patch isn't supposed to target different pixel densities and rather different content/screen sizes.

Countershading answered 7/2, 2013 at 15:17 Comment(1)
There is only one version (put in the drawable folder). So it seems the process you outlined explains the issue I am facing. Too bad, I though I could avoid creating images for all those different physical resolutions as well...Impetus
L
3

I checked out your 9 patch:

enter image description here

Why did you color your not used bounds white? It should be transparent. Because of this it is not recognized as a 9Patch:

enter image description here

Linkman answered 7/2, 2013 at 15:18 Comment(1)
White borders are my personal best practice as it avoids that nearly transparent pixels live on the outer borders. So far, I never had problems with this. And I double checked this: The red stripes (whatever they mean) also occur with completely transparent borders.Impetus

© 2022 - 2024 — McMap. All rights reserved.