88

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 3
    As far as I can see, VectorDrawable is the **androidification** of SVG files. I'm using **SVG**s for over an year through the use of **3rd party libraries**, though. ([android-svg](https://code.google.com/p/androidsvg/), [svg-android](https://code.google.com/p/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 Oct 24 '14 at 13:20
  • 7
    I have made online tool for converting SVG into xml resource vector (lollipop only) - http://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 Oct 27 '14 at 21:00
  • 1
    @Yuraj Wow, it's so cool ! great work ! And in such short time... Here, take my +1 . :) – android developer Oct 27 '14 at 22:57
  • Concerning http://inloop.github.io/svg2android/... It`s a very usefull tool but I`ve 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 Feb 20 '15 at 11:54
  • @AlexZezekalo You should write about it on its Github webpage: https://github.com/inloop/svg2android – android developer Feb 20 '15 at 20:13
  • I would like to add a modern library for SVGs: [Sharp](https://github.com/Pixplicity/sharp). – Paul Lammertsma Jan 21 '16 at 09:55
  • Android Support Library 23.2 now supports VectorDrawables API 7 onwards and AnimatedVectorDrawables API11 onwards. So the Answer to your question is now YES. – darshanz Feb 25 '16 at 04:40

9 Answers9

39

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.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • 3
    About the vectors support, isn't it enough to update the gradle plugin and use "vectorDrawables.useSupportLibrary = true" inside "defaultConfig" ? – android developer Mar 02 '16 at 07:52
  • 1
    yes, for **Gradle Plugin 2.0+** and for **Gradle Plugin 1.5** you need to add **-->** android { defaultConfig { generatedDensities = [] } // This is handled for you by the 2.0+ Gradle Plugin aaptOptions { additionalParameters "--no-version-vectors" } } – Amit Vaghela Mar 02 '16 at 07:59
  • 1
    Also, please show some code here. A link might be dead some day. You will get +1 for this effort :) – android developer Mar 02 '16 at 08:44
  • yes, for sure. will post code in shorttime on git and here. – Amit Vaghela Mar 02 '16 at 10:42
  • 2
    The `compile` statement above implies that the project uses `RecyclerView` which is not always the case. – razzledazzle Mar 09 '16 at 17:56
  • I assume this won't work on widgets and notifications, right? – android developer Mar 10 '16 at 08:38
  • Unfortunately VectorDrawables are still not working on Pre-Lollipop device. I'm on Support Library 23.4.0 and have both 'generatedDensities = []' and 'vectorDrawables.useSupportLibrary = true' called in defaultConfig{}. – AdamHurwitz Jul 20 '16 at 23:49
  • I implemented ‘AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);’ in the Activity with ‘vectorDrawables.useSupportLibrary = true’ on API Level 23.4.0, however VectorDrawables still crashes the app on pre-Lollipop devices. Hopefully AppCompat will fix this in the next library release. Very much looking forward to saving a ton of time with VectorDrawables. – AdamHurwitz Jul 21 '16 at 02:19
  • The best way is to use VectorDrawableCompat together with appCompat, which most of you should be using already. More details, please see https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.bbzbmzoc8 – Tenghui Zhu Nov 04 '16 at 20:49
  • Actual info on how to use appcompat vectors - http://stackoverflow.com/q/40678947/7045114 – Maksim Ostrovidov Nov 21 '16 at 00:21
  • so kitkat will support vector images now? – pallavi Jan 13 '17 at 13:46
  • Perhaps anyone can help... I am trying to convert ibb.co/SdZjjTH to xml using autotracer.org and inloop.github.io/svg2android. It works, but when I try to use it in my app, the app crashes. Can you check if it works for you? Thanks! – Yossi Feb 09 '20 at 16:25
14

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.

Community
  • 1
  • 1
razzledazzle
  • 6,900
  • 4
  • 26
  • 37
  • yes, i saw this. Thank you for the extended info. I assume that for system wide stuff (notifications, widgets,...) this is of course not available... – android developer Mar 09 '16 at 19:12
  • Certain that it should work. Its basically referencing drawables from that point on. Have you tried it? The example there sets a vector drawable to a `MenuItem`, which works fine. – razzledazzle Mar 10 '16 at 03:13
  • Are you sure it will work even for notifications and widgets? I'm talking about making it work on old Android versions. How could they handle vectors for this? They can't use the support library, no? – android developer Mar 10 '16 at 08:25
  • The whole point of using Support Library version is to make things work in the older versions. As documented, it works all the way back to API 7 and Animated vector drawables from API 11. – razzledazzle Mar 10 '16 at 08:27
  • Again, this works inside the app, but what if you try using them outside, like on notifications and widgets? I don't think it will work there, because I don't think you can make other apps (launchers) use the support library, let alone the notification bar. Have you tried it? – android developer Mar 10 '16 at 08:31
  • Well, this confirms it. They're unsupported. https://code.google.com/p/android/issues/detail?id=201958 – razzledazzle Mar 10 '16 at 17:58
  • Now it makes sense. Thank you. – android developer Mar 10 '16 at 19:05
12

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

RevanthKrishnaKumar V.
  • 1,855
  • 1
  • 21
  • 34
V_J
  • 1,081
  • 13
  • 33
  • That library is only compatible with API level 14 and above, whereas the Mr Vector library is compatible down to 7+. – donturner Sep 13 '15 at 16:16
  • @donturner: I agree with your point, but I don't think dev's are still developing app's for 7 API. – V_J Sep 14 '15 at 05:52
  • 4
    Maybe not API 7, but definitely API 10 (Gingerbread) as this accounts for 4.1% of the Android market (https://developer.android.com/about/dashboards/index.html). – donturner Sep 14 '15 at 13:27
8

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

David Ferrand
  • 5,357
  • 1
  • 33
  • 43
6

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.

gnuanu
  • 2,252
  • 3
  • 29
  • 42
3

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.

A-student
  • 785
  • 1
  • 5
  • 12
3

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

Mehlyfication
  • 498
  • 5
  • 9
  • Yes, it was published recently. However, it's using PNGs, instead of using the vectors, so if I change the size of the imageView that holds the image, it will be pixelated/blurry . – android developer Oct 21 '15 at 11:06
  • @android developer: You are partially right. This behaviour will occur, but this would be the same for VectorDrawables. Each VectorDrawable has a width and height defined within its XML (e.g. vector_drawable.xml). If this width/height is set to 50x50dp but you set the ImageView size to 100x100dp the result will still be blurred even if you're running Android 5.0 (and therefore are using the VectorDrawable instead of a auto-generated BitmapDrawable). The real benefit of a VectorDrawable over a common Bitmap is that it tackles screen-dpi into account and therefore won't blur even on 4k displays. – Mehlyfication Oct 22 '15 at 08:15
  • You mean that VectorDrawable doesn't show well when using a size that wasn't set for it? But it's a vectorized content.... Why would it work otherwise? – android developer Oct 22 '15 at 08:27
  • Does it generate pngs for Animated Vector Drawables too? – Louis CAD Feb 02 '17 at 16:48
  • No it won't create PNGs for that case. If you want to animate vector drawable on devices running Android version prior to Android 5.0 you might want to try VectorDrawableCompat: https://developer.android.com/reference/android/support/graphics/drawable/AnimatedVectorDrawableCompat.html – Mehlyfication Feb 02 '17 at 20:14
2

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.

George Mount
  • 20,708
  • 2
  • 73
  • 61
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LOG_TAG
  • 19,894
  • 12
  • 72
  • 105