VectorDrawable:无效的可绘制标签渐变 [英] VectorDrawable: Invalid drawable tag gradient

查看:84
本文介绍了VectorDrawable:无效的可绘制标签渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在使用Android Asset Studio从Zeplin导出的SVG文件生成矢量可绘制对象,除了几次以外,它都能正常工作.但是今天,当我尝试使用生成的矢量drawable时遇到了这个异常.

I have been using the Android Asset Studio to generate vector drawables from SVG files exported from Zeplin for a while and except a few times, it works fine. But today I got this exception when I was trying to use a generated vector drawable.

android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>

在同一堆栈跟踪中:

Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #0: invalid drawable tag gradient

我对Android中的Vector drawables和SVG不太了解. Vector drawables不支持渐变吗?是否有针对此问题的修复程序?我是否需要使用PNG?

I do not know a lot about Vector drawables and SVGs in Android. Are gradients not supported in Vector drawables? Is there any fix for this issue or do I need to use PNGs?

我正在使用支持库版本:

I am using Support Library version:

com.android.support:support-v4:26.1.0

com.android.support:support-v4:26.1.0

我正在使用

Android Studio 3.0

Android Studio 3.0

这是Android Asset Studio生成的VectorDrawable文件:

Here is the VectorDrawable file generated by Android Asset Studio:

<vector 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:aapt="http://schemas.android.com/aapt"
    android:width="360dp"
    android:height="110dp"
    android:viewportWidth="360.0"
    android:viewportHeight="110.0">
    <path
        android:pathData="M82,46H0v64h360V46h-81.88v-0.3h-26.21c-14.25,0 -38.69,-6.2 -46.95,-25.93C200.99,10.25 193.27,0.52 180,0.47c-13.27,-0.05 -20.04,9.24 -24.75,19.3 -8.22,17.55 -24.66,26.19 -49.34,25.93H82V46z"
        android:fillType="evenOdd">
        <aapt:attr name="android:fillColor">
            <gradient 
                android:startY="0.41999998688697815" 
                android:startX="0.0" 
                android:endY="110.0" 
                android:type="linear" 
                android:endX="360.0">
                <item android:offset="0.0" android:color="#FFCB09FF" />
                <item android:offset="1.0" android:color="#FF8A06FF" />
            </gradient>
        </aapt:attr></path>
</vector>

链接到原始SVG文件: https://drive.google. com/file/d/1Lj62xJv5IpYR5Lle7w1kMsFXh6y5PijK/view?usp = sharing

Link to original SVG file: https://drive.google.com/file/d/1Lj62xJv5IpYR5Lle7w1kMsFXh6y5PijK/view?usp=sharing

在Sublime上打开时的SVG内容:

SVG contents when opened on Sublime:

<svg xmlns="http://www.w3.org/2000/svg" width="360" height="110" viewBox="0 0 360 110">
    <defs>
        <linearGradient id="a" x1="0%" y1="0%" y2="100%">
            <stop offset="0%" stop-color="#CB09FF"/>
            <stop offset="100%" stop-color="#8A06FF"/>
        </linearGradient>
    </defs>
    <path fill="url(#a)" fill-rule="evenodd" d="M82 46H0v64h360V46h-81.876v-.299h-26.208c-14.25 0-38.69-6.198-46.946-25.93C200.99 10.252 193.27.52 180 .474c-13.27-.047-20.04 9.238-24.75 19.295-8.217 17.55-24.662 26.194-49.336 25.931H82V46z"/>
</svg>

推荐答案

属性

The property android:fillColor is only supported in OS 7.0+ ,

android:fillColor 指定用于填充小路.可能是 颜色,或者对于SDK 24+,是颜色状态列表或渐变颜色(请参见 GradientColor和GradientColorItem).如果将此属性设置为动画, 动画设置的任何值都将覆盖原始值.不 如果未指定此属性,则会绘制路径填充.

android:fillColor Specifies the color used to fill the path. May be a color or, for SDK 24+, a color state list or a gradient color (See GradientColor and GradientColorItem). If this property is animated, any value set by the animation will override the original value. No path fill is drawn if this property is not specified.

对于目录/drawable 中的旧版本,我们可以将矢量资产放置为没有渐变的位置,例如:

for older versions in directory /drawable we can place the vector asset without the gradients, for example:

<vector android:height="24dp" android:viewportHeight="651.95"
    android:viewportWidth="531.48" android:width="24dp"
    xmlns:aapt="http://schemas.android.com/aapt" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:pathData="M386.8,30.2c-48.5,0 -76.1,18 -121.1,18s-72.6,-18 -121.1,-18c-87.9,0 -144.7,83.3 -144.7,186 0,92.9 160,350.5 265.7,350.5 112.9,0 265.7,-257.6 265.7,-350.5C531.5,113.5 474.7,30.2 386.8,30.2Z">
        <!--<aapt:attr name="android:fillColor">
            <gradient android:endX="212457.73219299316"
                android:endY="440836.2612554932"
                android:startX="212457.73219299316"
                android:startY="92857.94223999024" android:type="linear">
                <item android:color="#FFFC3A11" android:offset="0.0"/>
                <item android:color="#FFDA0300" android:offset="1.0"/>
            </gradient>
        </aapt:attr>-->
    </path>
    ...
    ...

并位于带有渐变的/drawable-24 目录中

and inside /drawable-24 directory with gradients:

<vector android:height="24dp" android:viewportHeight="651.95"
    android:viewportWidth="531.48" android:width="24dp"
    xmlns:aapt="http://schemas.android.com/aapt" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:pathData="M386.8,30.2c-48.5,0 -76.1,18 -121.1,18s-72.6,-18 -121.1,-18c-87.9,0 -144.7,83.3 -144.7,186 0,92.9 160,350.5 265.7,350.5 112.9,0 265.7,-257.6 265.7,-350.5C531.5,113.5 474.7,30.2 386.8,30.2Z">
        <aapt:attr name="android:fillColor">
            <gradient android:endX="212457.73219299316"
                android:endY="440836.2612554932"
                android:startX="212457.73219299316"
                android:startY="92857.94223999024" android:type="linear">
                <item android:color="#FFFC3A11" android:offset="0.0"/>
                <item android:color="#FFDA0300" android:offset="1.0"/>
            </gradient>
        </aapt:attr>
    </path>
    ...
    ...

这篇关于VectorDrawable:无效的可绘制标签渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆