IntroductionAs the name implies, vector drawables are based on vector graphics. Vector graphics are a way of describing graphical elements using geometric shapes. This lets you create a drawable based on an XML vector graphic. Now there is no need to design different size image for mdpi, hdpi, xhdpi and etc. With Vector Drawable you need to create image only once as an xml file and you can scale it for all dpi and for different devices. This also not save space but also simplifies maintenance.
|Used to define a vector drawable|
|Defines a group of paths or subgroups, plus transformation information. The transformations are defined in the same coordinates as the viewport. And the transformations are applied in the order of scale, rotate then translate.|
|Defines paths to be drawn.|
|Defines path to be the current clip. Note that the clip path only apply to the current group and its children.|
Update build.gradle file.
If you are using v2.0 or above of the Gradle plugin, then add following code.
If you are using v1.5 or below of the Gradle plugin, then add following code.
Read Android Support Library 23.2 Release Notes for more info.
NOTE : Even with AppCompat, Vector Drawables wont work outside of your app in older android versions. For instance, you cannot pass vector drawables as Notification icons as they are handled by the system and not the app. See this answer for a workaround.
Importing SVG file as VectorDrawable
You can import an SVG file as a
VectorDrawable in Android Studio, follow these steps :
"Right-click" on the
res folder and select new > Vector Asset.
Select the Local File option and browse to your .svg file. Change the options to your liking and hit next. Done.
VectorDrawable Usage Example
Here’s an example vector asset which we’re actually using in AppCompat:
Using this drawable, an example
ImageView declaration would be:
You can also set it at run-time:
The same attribute and calls work for
VectorDrawable xml example
Here is a simple
VectorDrawable in this vectordrawable.xml file.