Android矢量图形<defs>不支持。如何解决?

17

| 尝试从 AndroidStudio 2.2 和 Ubuntu 14.04 中的本地 SVG 文件生成矢量资源时,我遇到了此错误。

Could not generate a preview
In icon.svg
ERROR@ line 6 <defs> is not supported
ERROR@ line 25 <use> is not supported

这里是SVG图标

我通过谷歌搜索发现这是因为AndroidStudio不支持<defs><use>等标签...
但我不知道该怎么解决?
我能否将<defs>更改为其他Android Studio支持的标签?
或者我永远无法从此SVG文件生成矢量资产?
任何帮助或建议都将不胜感激。


这完全取决于<defs><use>标签中的内容。但是如果没有看到您的文件中有什么,很难给出建议。 - Paul LeBeau
@PaulLeBeau 对不起我解释得不好。我已经添加了SVG图标。我敢肯定,当您将此SVG图标导入AndroidStudio时,您会遇到与我相同的错误。我已在另一台计算机上测试。 - Linh
2个回答

10

当我直接从Sketch导入SVG到Android Studio时遇到了同样的问题,我使用了这个工具将它转换为矢量可绘制对象,然后再导入。


将Sketch中的SVG导出为Android格式,如果您未使用Android API 24及以上版本,则此方法非常有用。 - iamsujan

9
你的SVG使用了一个<linearGradient>元素,并且为了产生微弱的阴影,它使用了一个SVG滤镜(<filter>)。然而,VectorDrawables并不支持这两个元素。因此,任何解决方案都需要移除它们。
看起来这个文件是在Sketch中创建的。你可以尝试在Sketch中删除这些元素,然后再次导入SVG文件。
如果仍然不能正常工作,则可能需要手动修复其他未被导入器支持的元素:<use>。好消息是它很容易解决。
下面提供了一个手动编辑过的文件版本,它修复了这三个问题。它应该能够在Android Studio中导入,但我不知道它是否符合你的美学需求。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="353px" height="475px" viewBox="0 0 353 475" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <!-- Generator: sketchtool 40.2 (33826) - http://www.bohemiancoding.com/sketch -->
    <title>3765E978-B278-427B-8571-70DE79D99C8A</title>
    <desc>Created with sketchtool.</desc>
    <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <g id="LoadingScreen" transform="translate(-370.000000, -455.000000)">
            <g id="icon" transform="translate(302.881496, 430.476899)">
                <g id="Page-1-Copy-3" transform="translate(227.267540, 231.515653) rotate(-40.000000) translate(-227.267540, -231.515653) translate(82.767540, 51.515653)">
                    <g id="Combined-Shape">
                        <path stroke="#55FFD9" stroke-width="1" fill="#49D1B7" fill-rule="evenodd"
                              d="M186.685057,69.3951579 C179.071738,84.054677 167.196464,93.5829106 147.566191,86.4324469 C145.5069,85.6807637 143.143294,85.6854176 140.908382,85.4719208 C110.344523,82.5443919 101.488745,66.5911886 116.541777,40.0162683 C124.759532,25.5078922 135.844381,13.0321318 151.386964,5.38509524 C176.103451,-6.77934919 195.596784,4.90406969 195.936794,31.9247221 C196.09814,44.884376 193.040127,55.9872472 186.685057,69.3951579 L186.685057,69.3951579 Z M198.595037,119.672673 C182.675646,106.53093 181.93282,99.7311426 194.798418,84.9441152 C210.815159,66.5375926 231.906487,60.2048217 243.950958,70.1893157 C255.533227,79.7914354 252.958093,97.0539863 237.290073,114.861111 C222.808075,131.31966 214.025927,132.411187 198.595037,119.672673 L198.595037,119.672673 Z M261.701593,172.524646 C256.255382,175.618319 247.028302,174.835442 241.296061,165.817955 C230.52687,148.874141 231.029057,142.675217 248.302041,132.303868 C258.061419,126.443308 268.19848,124.565822 276.849824,132.235549 C286.713683,140.98316 282.007021,150.347416 276.816132,159.049853 C272.613406,166.093343 261.701593,172.524646 261.701593,172.524646 L261.701593,172.524646 Z M258.884249,185.876613 C265.445061,178.927297 275.146113,178.821092 281.280117,183.8411 C290.291923,191.219766 290.497384,203.145311 281.829513,210.911347 C265.175566,225.832917 237.744852,208.265824 258.884249,185.876613 L258.884249,185.876613 Z M253.914854,238.407128 C259.379321,233.168583 266.48379,232.575328 271.037507,236.310514 C277.728699,241.7988 277.025625,250.455585 270.795386,257.209223 C258.269501,270.789008 235.658358,255.908285 253.914854,238.407128 L253.914854,238.407128 Z M0.471927459,334.579649 C-0.840210761,352.465266 12.3567324,362.93098 29.9512025,357.954605 L143.456907,325.850939 C195.727834,313.150715 236.519224,256.512569 230.761952,204.648157 C227.6544,176.61325 213.571088,151.367964 191.116546,133.562925 C168.660361,115.756584 140.515656,107.517709 111.868627,110.365293 C58.8564726,115.631895 10.7533507,166.485529 8.95278386,218.977994 L0.471927459,334.579649 L0.471927459,334.579649 Z M149.592296,185.327725 C166.515393,198.746676 169.20872,223.050038 155.605641,239.498964 C142.002561,255.94789 117.165239,258.42118 100.242142,245.002229 C83.3190454,231.583278 80.6257184,207.279916 94.2287979,190.83099 C107.831877,174.382064 132.669199,171.908774 149.592296,185.327725 L149.592296,185.327725 Z"></path>
                    </g>
                </g>
            </g>
        </g>
    </g>
</svg>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接