将SVG转换为Android矢量图形。

3

我正在尝试将作为命令行工具(abcm2ps)输出的SVG文件转换为Android矢量可绘制图形,但是遇到了问题。

我的处理过程如下:

  1. 使用上述工具创建原始SVG文件
  2. 通过Inkscape运行SVG文件以修复页面大小
  3. 再次通过Inkscape运行结果以保存为纯SVG格式(在某个地方读到说这可能会修复问题,但并没有帮助)
  4. 尝试导入SVG文件到Android Studio,但无法成功

我得到的错误是一些无法找到引用ID的错误。我查看了XML文件,并注意到此错误仅对包含引用的引用产生。例如:

<defs
   id="defs6633">
  <path
     id="usharp"
     class="fill"
     d="m 136,-702 v 890 h 32 v -890 m 128,840 h 32 V -750 H 296 M 64,-464 v 116 l 338,-96 V -560 M 64,-118 V -2 l 338,-98 v -114"
     inkscape:connector-curvature="0"
     style="fill:currentColor" />
  <use
     id="sh0"
     transform="matrix(0.018,0,0,0.018,-4,5)"
     xlink:href="#usharp"
     x="0"
     y="0"
     width="100%"
     height="100%" />
</defs>
<use
   x="44.5"
   y="20"
   xlink:href="#sh0"
   id="use6635"
   width="100%"
   height="100%" />

如您所见,对象use6635引用了sh0,而后者又引用了usharp
然而,其他一些引用对象不引用其他对象的地方是有效的。
这是转换器已知的限制吗?有没有一种方式(最好是命令行工具)可以扁平化引用层次结构,以便只引用不引用其他对象的对象?
编辑:好吧,将其转换为EPS,然后再转换为SVG会创建可处理无上述警告的SVG文件 - 我现在得到的警告是说不支持缩放描边宽度...

尝试使用内置于Android Studio中的矢量资产工具(Vector Asset Studio)。https://developer.android.com/studio/write/vector-asset-studio - Jim Andreas
4个回答

3

你应该尝试使用Shape Builder,我遇到了一个相关问题,它解决了我的问题,希望对你也有所帮助!


很不错的尝试,但是并没有帮助。它说我的SVG文件无法导入。 - Thorsten Dittmar
好的,我比昨天又进了一步。将我的工作流程更改为先创建一个EPS文件,然后使用Inkscape从该EPS创建一个SVG,这样可以使您链接的网站正确地读取SVG。Android Vector Studio现在也可以读取它,但会警告无法缩放线宽... - Thorsten Dittmar

1

最近几天我一直在使用这个网站,取得了很好的效果。只需上传你的SVG文件,即可获取可绘制的XML代码。[https://svg2vector.com/][1]


0

有一个内置的Android转换器的命令行版本可以从Alex Lockwood这里下载,工具的下载链接在Google Drive上

运行该工具需要Java JRE支持;你可以在Windows系统中设置JAVA_HOME为你的Android Studio JRE,然后再运行它。

set JAVA_HOME=C:\Program Files\Android\Android Studio\jre
call "%USERPROFILE%\Applications\vd-tool\bin\vd-tool.bat" %*

使用方法:

./vd-tool -c -in svgs/ -out vectors/

帮助:

Converts SVG files to VectorDrawable XML files.
Displays VectorDrawables.
Usage: [-c] [-d] [-in <file or directory>] [-out <directory>] [-widthDp <size>] [-heightDp <size>] [-addHeader]
Options:
  -in <file or directory>:  If -c is specified, Converts the given .svg file
                            to VectorDrawable XML, or if a directory is specified,
                            all .svg files in the given directory. Otherwise, if -d
                            is specified, displays the given VectorDrawable XML file
                            or all VectorDrawables in the given directory.
  -out <directory>          If specified, write converted files out to the given
                            directory, which must exist. If not specified the
                            converted files will be written to the directory
                            containing the input files.
  -c                        If present, SVG files are converted to VectorDrawable XML
                            and written out.
  -d                        Displays the given VectorDrawable(s), or if -c is
                            specified the results of the conversion.
  -widthDp <size>           Force the width to be <size> dp, <size> must be integer
  -heightDp <size>          Force the height to be <size> dp, <size> must be integer
  -addHeader                Add AOSP header to the top of the generated XML file
Examples:
  1) Convert SVG files from <directory> into XML files at the same directory and visualize the XML file results:
  vd-tool -c -d -in <directory>
  2) Convert SVG file and visualize the XML file results:
  vd-tool -c -d -in file.svg
  3) Display VectorDrawable's XML files from <directory>:
  vd-tool -d -in <directory>

注意:这个版本在处理一些由Inkscape生成的SVG文件时存在问题,这些文件包含引用其他渐变定义的渐变;有关更多信息,请参见this question;这只是内置Android工具的命令行版本。

0

感谢您的回答。虽然一次转换大量文件的能力与命令行工具一样好,但是这个工具也不支持缩放笔画宽度。无论如何,还是感谢您的帮助! - Thorsten Dittmar

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