Automatically solve Android build Error:Frame pixels must be either solid or transparent (not intermediate alphas). - Found at pixel #4 along top edge

后端 未结 4 1942
太阳男子
太阳男子 2020-12-14 03:11

Android Studio (using SDK 19, 21 or 22) shows an error that Eclipse ADT (using SDK 19) does not:

Error:9-patch image D:\\Workspaces....\\res\\drawable

4条回答
  •  星月不相逢
    2020-12-14 04:08

    How automatically solve the issue (without checking all images in different resolutions)

    Not possible. Since you want the .png to behave like a nine-patch (expand along the edges, not stretch the whole bitmap), you're going to have to format them as such ergo you have to modify the files.

    Proposed alternative

    Since the shape is so simple you'd be better off deleting all variants of this file (saving space, time and headache in the process) and create /res/drawable/btn_bg_common_press.xml drawable with following contents:

    
    
        
        
        
    
    

    You can use dimen and color resources instead of hardcoded values. Additionally you might want to add the padding element inside shape

    
        
    
    

    and/or wrap the shape element inside an inset element.

    
        
    
    

    If you do that, the drawable will have implied padding and you won't have to set it when styling widgets. Since you have multiple state drawables for the button I suggest you convert all of them to XML to make sure the line thickness and corners match.

    Despite the root element's name the actually inflates to GradientDrawable (meaning you can fill it with gradient instead of solid color). Review GradientDrawable docs for all its options. Do not ever use ShapeDrawable programmatically, it just doesn't work.

    Analyzing 9-patches

    Consider the three following enlarged images.

    Nine-patch samples

    Candidate #1 is a nine-patch. It has reserved 1px on each side for stretch and padding specification. This one will behave as an ordinary bitmap when stretched. If the width will be greater than height, so will the border thickness on sides. The border will scale proportionally.

    Candidate #2 is also a nine-patch and is actually saying it's going to stretch everything besides 1px border and will have implied padding of 3px on each side. It will have a nice crisp 1px border when stretched.

    Candidate #3 is NOT a nine-patch. It scales the same way as #1.

    Now let's take a look at enlarged version of the image you included in OP:

    Enlarged image from OP

    As you can see it's not a nine-patch, so it won't get interpreted as one and the build tools are kind enough to warn you of that. Even if older build tools were more forgiving and added transparent 1px on each side for you, the result would have behaved like an ordinary bitmap, meaning when stretched it would look like specimen A instead of expected result specimen B.

    Comparison of a bitmap and a nine-patch

    Here's more reading on nine-patches. This explains what the extra 1px on each side is used for.

提交回复
热议问题