In the previous part, we discussed
the BetterVectorDrawable library , which implements
VectorDrawable for Android 4.0+.
In this part we will talk about converting images from SVG to vector drawable XML.
When Android reads XML resources, it ignores attributes that are not supported by the current device. This means that unsupported attributes in vector drawable XML will have to be duplicated in the namespace.
schemas.android.com/apk/res-auto
schemas.android.com/apk/res-auto
so that BetterVectorDrawable can read them. For example:
<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:better="http://schemas.android.com/apk/res-auto" android:width="100dp" android:height="100dp" android:viewportHeight="100" android:viewportWidth="100" better:viewportHeight="100" better:viewportWidth="100"> …
In this case, it is necessary to duplicate only unsupported attributes (the list can be viewed
here ; see attributes without the
android:
prefix
android:
.
Fortunately, all this is done for us by the converter and we do not even need to think about it.
')
SVG to VectorDrawable Converter
This converter allows you to create vector drawable resources for both Android 5.0+ and the BetterVectorDrawable library. This is controlled by a command line argument.
The converter is a cross-platform console application: you can run it on Windows or OS X. You need to install dependencies for its work, this will be discussed later. The converter itself is distributed as an archive with an executable file, which can be downloaded
here .
The archive needs to be unpacked and, opening the console on Windows (terminal on OS X), change the current directory to the directory with the converter.
The command that starts the conversion looks like this:
[mono] svg2vd.exe -i in_dir/\* -o out_dir [--lib BetterVectorDrawable]
Optional parameters are indicated in square brackets (although they are used without brackets):
mono
should be specified if you are running the converter on OS X--lib BetterVectorDrawable
should be specified if you want unsupported attributes to be duplicated in the output XML, i.e. convert SVG to vector drawable XML for BetterVectorDrawable library, otherwise the converter will create vector drawable resources for Android 5.0+.
The remaining parameters, I think, are clear and so; The argument
--help
displays their descriptions.
Dependency installation
If you have windows
Microsoft .NET Framework
If you have Windows 8 or higher, then this item can be skipped.
The system must have .NET Framework 4.5+ installed. You can check the version of the installed .NET Framework according to
this instruction . Download .NET Framework 4.5
here .
Inkscape
Inkscape is an open source free vector graphics editor. Installing under Windows is no different from the usual installation of the program. Download
here .
If you have OS X
You need OS X 10.7 or better.
Mono
Download Mono
here . Installation took me without difficulty.
Inkscape
Inkscape is an open source free vector graphics editor. To install under OS X, you first need to install
XQuartz , then you need to restart the computer or exit and log in again.
Download Inkscape
here . Make sure that after installation Inkscape runs freely. I needed to unblock it in
System Preferences ... →
Security & Privacy →
General . You need to click
Click the lock to make changes and unlock the launch of Inkscape.
If you have any difficulties, you can write to me. It is better to report problems with the converter to developers on
GitHub by adding a link to the problem SVG file.
Thanks for attention.
Update 10.09.2015: An
online version of the converter has appeared .