Search code examples
androiduser-interfacebuttonbackgroundnine-patch

Creating Nine (9) Patch png files with Gradients


I want to create a nine patch button background with a gradient for my android app. I was wondering how gradients work. I would think that the os would have to figure out how to apply the gradient as the button stretched and I'm guessing that information (a graphic vector equation) is not available in the 9 patch file? So is there a solution for this problem? Also, what about dithering, etc. I have created a few nine-patch button backgrounds, but none of them have a gradient. yet. Thanks in advance.


Solution

  • Why do you want to use a 9-patch for that? 9-patches stretch an area of an image by copying/duplicating pixels. That will not work well with gradients. Have you considered using a GradientDrawable instead?

    <shape android:shape="rectangle">
        <gradient android:startColor="..." android:endColor="..." />
    </shape>
    

    See shape drawables.