88
votes

Background

I've noticed that Android now supports some kind of vector drawing, via a class called "VectorDrawable" (and also AnimatedVectorDrawable, BTW). I've found about it by looking at what's new on Android-Studio.

I wonder if this would be the end of having to put multiple files into multiple folders (mdpi, hdpi, xhdpi, etc). That would be great and might minimize apps sizes on some cases.

The questions

I'd like to ask a few questions regarding this new class:

  1. Is it possible to use it in older Android versions, maybe via a library of even the support library of Google?

  2. I'm not familiar with how it works, but can Lollipop handle SVG files? Can it do anything that is achievable on SVG files?

  3. Is there any sample/tutorial/video of using it, other than the documentation I've found?

9
As far as I can see, VectorDrawable is the androidification of SVG files. I'm using SVGs for over an year through the use of 3rd party libraries, though. (android-svg, svg-android and such). There are similarly named ones. Each one differs from the other ones, so, choose accurately depending on your needs.Phantômaxx
I have made online tool for converting SVG into xml resource vector (lollipop only) - inloop.github.io/svg2android - It was not tested so much still in early alpha. It support simple svg files (as android supports), I have tested It with inkscape - drawed some shapes (rect,circle,spiral...), select all then "Path->Object to path", exported into *.svg and dropped into site, generated xml and works fine.Yuraj
@Yuraj Wow, it's so cool ! great work ! And in such short time... Here, take my +1 . :)android developer
Concerning inloop.github.io/svg2android... Its a very usefull tool but Ive got improper result in such case and I don`t know what is wrong... Hmmm, for further release I wish this tool would notise about bad format... If I used pure svg-files with 3-party libs everything is Ok...Alex Zezekalo
@AlexZezekalo You should write about it on its Github webpage: github.com/inloop/svg2androidandroid developer

9 Answers

39
votes

UPDATE ON March 2016

By Android Support Library 23.2.1 update, Support Vector Drawables and Animated Vector Drawables. (you can also use latestone for the same)

Please update version of a library in gradle file.

compile 'com.android.support:recyclerview-v7:23.2.1'

Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices, both VectorDrawable and AnimatedVectorDrawable are now available through two new Support Libraries support-vector-drawable and animated-vector-drawable. new app:srcCompat attribute to reference vector drawables .

Check source on github with some sample examples.

Changes for v7 appcompat library:

Reverted dependency on vector assets so that developers using the appcompat library are not forced to use VectorDrawable and its associated build flags.

14
votes

Update 2: They enable it again in Support Library 23.4.0:

For AppCompat users, we’ve added an opt-in API to re-enable support Vector Drawables from resources (the behavior found in 23.2) via AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) - keep in mind that this still can cause issues with memory usage and problems updating Configuration instances, hence why it is disabled by default.

Check this 23.4.0 available now

Update: This doesn't work from version 23.3.0 Check here for details. Proxy drawables don't work. app:srcCompat and setImageResource() work, however.


Vector Drawable support is available from the Support Library of version 23.2 and beyond. However, to properly use those drawables, they must be referenced indirectly.

First step would be to bump the AppCompat version.

compile 'com.android.support:appcompat-v7:23.2.0'

Second enable Vector Drawable support. If using Gradle plugin, 2.0+

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
   }  
}

Otherwise

android {  
   defaultConfig {  
     generatedDensities = []  
   }  

   aaptOptions {  
     additionalParameters "--no-version-vectors"  
   }  
}

Third, refer to the linked answer.

12
votes

You can try this support library. It supports VectorDrawable and AnimatedVectorDrawable introduced in Lollipop with fully backwards compatibility.

8
votes

To complement some of the answers here: yes, you can get support for VectorDrawables pre-Lollipop, at least partial.

How partial? It depends - I've made this diagram to help (valid for Support Library 23.4.0 to - at least - 25.1.0).

VectorDrawable cheatsheet

6
votes

Unfortunately, at this point of time VectorDrawable and AnimatedVectorDrawable are not available in support library. But to avail this feature in Pre-Lollipop versions, you can use the unofficial backport called MrVector.

MrVector is available in Github and it will support android versions 7+.

From the official Readme

To add MrVector dependency add the following line to your build.gradle dependencies block.

compile 'com.telly:mrvector:0.2.0'

To create the drawable from the vector XML,

Drawable drawable = MrVector.inflate(getResources(), R.drawable.vector_android);

Hope this helps.

3
votes

If you are using VectorDrawable, Android Studio will automatically generate according PNG files (based on your XML files) for Pre-Lollipop versions.

Note that those generated PNG files are considered BitmapDrawables instead of VectorDrawables on devices running API below 21 and therefore can't be animated or similar on those devices.

See "backwards compatibility" for further details: http://android-developers.blogspot.co.at/2015/09/android-studio-14.html

3
votes

Lollipop cannot handle SVG files without third-party libs.

The best solution I found is the BetterVectorDrawable lib together with the SVG to VectorDrawable Converter.

BetterVectorDrawable is the VectorDrawable implementation for Android 4.0+ with configurable fall-back behavior on Android 5.0+.

SVG to VectorDrawable Converter is the batch converter of SVG images to Android VectorDrawable XML resource files. Online version

Links point to readmes, which provide enough information on how to use the lib and the converter.

2
votes

There are no VectorDrawables in the support library at this time.

Funkystein is right -- VectorDrawable is similar to SVG, only supporting the features of vector drawing that are in highest demand so that android can focus on performance. pathData, for example has the same format as SVG's "d" string.

1
votes

The great news is that Google released Android Support Library 23.2 Support Vector Drawables and Animated Vector Drawables!

But thanks go to the people who ported this library before Google!

This is where the AppCompat libraries are great, they can bring many of the new features of Android back to much earlier versions. With the newly implemented VectorDrawable class, developers can now use vector images all the way back to API 7 (Android 2.1 Eclair). Animated vectors are a bit more limited, going only as far back as API 11 (Android 3.0 Honeycomb), but that still encompasses more than 97% of devices in active use today

Guide to use:

Refer "age-of-the-vectors" by @chrisbanes