user462982
user462982

Reputation: 1645

9 patch image is scaled at the wrong positions

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?

Upvotes: 5

Views: 765

Answers (2)

Ahmad
Ahmad

Reputation: 72533

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

Upvotes: 3

viirus
viirus

Reputation: 1047

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.

Upvotes: 6

Related Questions