After upgrading to 3.0 Android Studio has started to complain about long paths in vector drawables.
The warning says:
Very long vector path (7958 characters), which is bad for performance. Considering reducing precision, removing minor details or rasterizing vector. less... (⌘F1) Using long vector paths is bad for performance. There are several ways to make the pathData shorter: * Using less precision * Removing some minor details * Using the Android Studio vector conversion tool * Rasterizing the image (converting to PNG)
The documentation https://developer.android.com/guide/topics/graphics/vector-drawable-resources.html shows us how to use vector drawables in our apps, and recommends it over png's etc.
I have both been using the Android Studio Vector Conversion Tool and this excellent service for converting SVG's to vector drawables: http://inloop.github.io/svg2android/
Are there other services that does more to reduce vector drawable complexity? Where can I find guidelines on how 'advanced' my vector drawables can be?
Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.
A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information. The major advantage of using a vector drawable is image scalability.
If you want to use some icons that are not present in the list of predefined icons, then you can add your own icon by selecting the Asset Type as Local File(SVG, PSD) and then select the path of your file by clicking on Path. After that click on Next > Finish. Now your icon will be added to your res/drawable folder.
Try optimizing the vector drawable using avocado
! It should help reduce the complexity of your paths.
My path was around 1800 and i used svg-path-editor to lower to around 1000. Still showed the warning but quite close to the max 800
I am sure there is a better way to use this tool but i just copied the pathData
from android studio xml to the Path box, pressed Round and got the result back to the xml
If your path is more than 2000, consider converting to png (with mdpi, hdpi, xhdpi...)
There's an extra step you can add before using svg2android
, which is running it through svgo
An example pipeline that I use looks like (note that instead of the web tool, I'm using svg2vectordrawable)
~$ svgo image.svg --config=config -o image.svg.optimized
~$ s2v "image.svg.optimized image.xml
My config
file looks like (you can play around with it to match your needs):
"plugins": [
{
"convertPathData": {
"leadingZero": false,
"floatPrecision": 2
}
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With