Android AnimatedVectorDrawable With Examples

In this tutorial, we’ll be discussing AnimatedVectorDrawable and implementing it in various ways in our android application. Having an idea of VectorDrawables would make the below article easier to understand.

AnimatedVectorDrawable

AnimatedVectorDrawable class was introduced since API 21 and is used to animate Vector Drawables beautifully and easily.

Following are some of the things that you can perform with AnimatedVectorDrawable:

  • Rotate, Scale, Translate VectorDrawables
  • Animate the VectorDrawable such as fill color etc.
  • Draw paths and do Path Morphing

Typically to animate the Vector Drawable we define the animations using the ObjectAnimator class.

ShapeShifter is a popular tool to visually create an AnimatedVectorDrawable.

For this, we need to import a Vector Asset/ SVG file.

We’ve got a sample vector drawable xml file named ic_settings.xml as shown below:

Let’s import this in ShapeShifter to create an AnimatedVectorDrawable:

android animatedvectordrawable shapeshifter demo

Firstly, we need to add the vector drawable inside a group layer in order to add animations to it.

We’ve set the vector drawable to rotate by 270 degrees.
You must set the pivotX and pivotY about which the VectorDrawable would rotate.

The background color of the VectorDrawable isn’t always imported in the ShapeShifter correctly. So you need to set that again there.

Following are some of the properties that can be set on the AnimatedVectorDrawable:

  • rotation
  • pivotX
  • pivotY
  • scaleX
  • scaleY
  • translateX
  • translateY
  • pathData
  • fillColor
  • strokeColor
  • strokeWidth
  • strokeAlpha
  • fillAlpha
  • trimPathStart
  • trimPathEnd
  • trimPathOffset

Now from the ShapeShifter, click Export – AnimatedVectorDrawable to generate the xml version of the AnimatedVectorDrawable:

avd_settings.xml

We’ve set the long path data as a string resource in the strings.xml file.

The name attribute of the target element in the AnimatedVectorDrawable corresponds to the name of the group, which is animated. The animation attribute takes the animator class which is object animator here.

To make the AnimatedVectorDrawable animate forever, we’ve added a repeatCount to infinite in the ObjectAnimator tag.

Now that we have got the AnimatedVectorDrawable we can set it on our ImageView(any related view) by:
android:src="https://www.journaldev.com/23261/@drawable/avd_settings".

Still the animated vector drawable won’t animate!

In order to make it animate, we need to do:

Animatable is an interface that contains methods to handle animations on drawables.
Methods like – start(), stop(), isRunning()

Setting AnimatedVectorDrawable Programmatically

this is the Context.

Instead of creating the whole animated vector drawable in a single xml file we can set the objector animators separately as well.

In the following section, we’ll be creating different types of AnimatedVectorDrawable

Project Structure

android-animated-vector-drawable-project-structure

DON’T forget to enable vector support in your app’s build.gradle:

Code

The code for the activity_main.xml layout is given below:

We’ve already seen the avd_settings.xml file.

Let’s see avd_jd.xml next.

The ic_jd.xml that we’ve written is:

The avd_jd.xml code generated from the ShapeShifter is:

In the above XML code, we’ve done scale, animate and rotate separately on both the paths.
Notice that each path has a name property which is linked to the respective target.

The XML properties are self-explanatory. And once you get a hang of it, you can write the AnimatedVectorDrawable XML code directly without the need of ShapeShifter.

The avd_cross2tick.xml and avd_tick2cross.xml are used to transform a tickmark vector drawable into a cross vector drawable. Thus it transforms paths.
The codes for the object animators are separately put inside the animator folder inside the res directory.

You can find their complete implementations in the source code/Github repository at the end of this tutorial. We’re skipping this due to space constraints.

The code for the MainActivity.java is given below:

Here we’ve set the AnimatedVectorDrawable on two ImageView and two FloatingActionButtons.

In order to listen to animation start and end, we can register Animation Callbacks on the AnimatedVectorDrawable as:

Note: registerAnimationCallback works on Android M(23) and above only.

The output of the application in action is :

android animatedvectordrawable output

This brings an end to this tutorial. You can download the project from the link below or check out the GitHub repository for the complete XML codes of all the AnimatedVectorDrawables.

By admin

Leave a Reply