我已经使用Android Asset Studio一段时间了,从Zeplin导出SVG文件生成矢量图标。除了偶尔出现的一些问题,它都工作得很好。但今天当我尝试使用生成的矢量图标时,出现了一个异常。
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中的矢量可绘制对象和SVG并不了解。在矢量可绘制对象中不支持渐变吗?有没有解决这个问题的方法或者我需要使用PNG格式的图片?
我正在使用支持库版本:
com.android.support:support-v4:26.1.0
我正在使用
Android Studio 3.0
这是由Android Asset Studio生成的VectorDrawable文件:
<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
在Sublime中打开的SVG内容:
<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>
minSdkVersion 21
的项目中将该资源用于ImageView
上,在运行Android 7.1的Nexus 9上没有问题。你在哪个版本的Android上测试时出现了崩溃? - CommonsWare<gradient>
支持(因为来自你的SVG的资产在那里有效)。 - CommonsWare