9-patch image error in Android

前端 未结 14 1048
野性不改
野性不改 2020-12-04 12:53

Whenever I try to add a 9-patch image to the \\drawable folder in my project, I get the same error:

ERROR: 9-patch image C:\\...\\res\\drawable\         


        
相关标签:
14条回答
  • 2020-12-04 13:36

    Just for the record: For me it turned out that this error came from a black border line on the right hand site and bottom side. So I had a black border on every side (top, right, bottom and left). Removing the redundant right and bottom line worked like a charm.

    0 讨论(0)
  • When the error comes up it opens the image in android studio as well. All I did to fix this was extend any of the patch borders by a single drawn pixel. I re-ran and it worked. Not sure why, but I repeat tested this with several images.

    0 讨论(0)
  • 2020-12-04 13:37

    Your 9-patch can be wrong.

    Maybe you strip 9-patches one-pixel borders away and information gained from them is not available. Try to add a bottom line and a right line for text too. I think it will help to solve your problem.

    0 讨论(0)
  • 2020-12-04 13:39

    When we nine patch any image it creates a black line to its border. That border should be transparent or completely solid. If it is not, this error will come.

    So the solution is to increase the width and height by two pixels. (In Photoshop increase the size of the canvas, not the image.)

    0 讨论(0)
  • 2020-12-04 13:41

    Now there are two PNG crunchers in the Android build tool, AAPT and a Java cruncher. Both checks for malformed 9 patch images.

    I've looked into the source code of the build tool. There is no option to disable compression on 9 patch image or ignore malformed ones. At least not options available to users.

    So here is a simple script to replace AAPT:

    https://gist.github.com/ksc91u/37513796b2cec37bb3c5

    When called to compress a 9 patch image, it will simply copy the file, otherwise, it will launch the real AAPT with @ARGV.

    0 讨论(0)
  • 2020-12-04 13:41

    For me the problem seems to be that I created the 9 patch myself. I thought it was enough to have a 1 px border on the left and top edges, but you need to make sure that the image has a 1 px border on each edge.

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