Vector Drawables Overview - Android Developers
Vector Drawables Overview - Android Developers
This page and the video below provide an overview of how to create vector drawables in XML. Android Studio
can also convert SVG les to the vector drawable format, as described in using Add multi-density vector
graphics (/studio/write/vector-asset-studio).
Example XML
Here is a sample VectorDrawable XML le that renders an image of a battery in the charging mode.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group
android:name="rotationGroup"
android:pivotX="300.0"
android:pivotY="300.0"
android:rotation="45.0" >
<path
android:name="vectorPath"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vd" >
<target
android:name="rotationGroup"
android:animation="@anim/rotation" />
<target
android:name="vectorPath"
android:animation="@anim/path_morph" />
</animated-vector>
Animator XML les that are used in the AnimatedVectorDrawable's XML le: rotation.xml and
path_morph.xml
<objectAnimator
android:duration="6000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360" />
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="3000"
android:propertyName="pathData"
android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z"
android:valueTo="M300,70 l 0,-70 70,0 0,140 -70,0 z"
android:valueType="pathType"/>
</set>
Single XML le
By using this approach, you can merge the related XML les into a single XML le through the XML Bundle
Format. At the time of building the app, the aapt tag creates separate resources and references them in the
animated vector. This approach requires Build Tools 24 or higher, and the output is backward compatible.
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt">
<aapt:attr name="android:drawable">
<vector
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:name="root"
android:strokeWidth="2"
android:strokeLineCap="square"
android:strokeColor="?android:colorControlNormal"
android:pathData="M4.8,13.4 L9,17.6 M10.4,16.2 L19.6,7" />
</vector>
</aapt:attr>
<target android:name="root">
<aapt:attr name="android:animation">
<objectAnimator
android:propertyName="pathData"
android:valueFrom="M4.8,13.4 L9,17.6 M10.4,16.2 L19.6,7"
android:valueTo="M6.4,6.4 L17.6,17.6 M6.4,17.6 L17.6,6.4"
android:duration="300"
android:interpolator="@android:interpolator/fast_out_slow_in"
android:valueType="pathType" />
</aapt:attr>
</target>
</animated-vector>
Vector drawables backward compatibility solution
To support vector drawable and animated vector drawable on devices running platform versions lower than
Android 5.0 (API level 21), VectorDrawableCompat
(/reference/androidx/vectordrawable/graphics/drawable/VectorDrawableCompat) and
AnimatedVectorDrawableCompat
(/reference/androidx/vectordrawable/graphics/drawable/AnimatedVectorDrawableCompat) are available through two
support libraries: support-vector-drawable and animated-vector-drawable, respectively.
Android Studio 1.4 introduced limited compatibility support for vector drawables by generating PNG les at
build time. However, the vector drawable and animated vector drawable support Libraries offer both exibility
and broad compatibility — it's a support library, so you can use it with all Android platform versions back to
Android 2.1 (API level 7+). To con gure your app to use vector support libraries, add the vectorDrawables
element to your build.gradle le in the app module.
Use the following code snippet to con gure the vectorDrawables element:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_add" />
Path Morphing (PathType evaluator) Used to morph one path into another path.
Path Interpolation Used to de ne a exible interpolator (represented as a path) instead of the system-
de ned interpolators like LinearInterpolator.
Move along path The geometry object can move around, along an arbitrary path, as part of an
animation.
The following XML les demonstrate the approach of using multiple XML les to animate a vector graphic.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group
android:name="rotationGroup"
android:pivotX="300.0"
android:pivotY="300.0"
android:rotation="45.0" >
<path
android:name="vectorPath"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>
<objectAnimator
android:duration="6000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360" />
Single XML le
The following XML le demonstrates the approach of using a single XML le to animate a vector graphic. At
the time of building the app, the aapt tag creates separate resources and references them in the animated
vector. This approach requires Build Tools 24 or higher, and the output is backward compatible.
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt">
<aapt:attr name="android:drawable">
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="64dp"
android:height="64dp"
android:viewportWidth="600"
android:viewportHeight="600">
<group
android:name="rotationGroup"
android:pivotX="300"
android:pivotY="300"
android:rotation="45.0" >
<path
android:name="vectorPath"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>
</aapt:attr>
<target android:name="rotationGroup">
<aapt:attr name="android:animation">
<objectAnimator
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360"
android:duration="6000"
android:interpolator="@android:interpolator/fast_out_slow_in" />
</aapt:attr>
</target>
</animated-vector>
Content and code samples on this page are subject to the licenses described in the Content License (/license). Java is a registered
trademark of Oracle and/or its a liates.