Advertisement
  1. Code
  2. Android SDK
Code

Using Android's VectorDrawable Class

Introduction

While Android does not support SVGs (Scalable Vector Graphics) directly, Lollipop introduced a new class called VectorDrawable, which allows designers and developers to draw assets in a similar fashion using only code.

In this article, you will learn how to create a VectorDrawable with XML files and animate them in your projects. This is only supported for devices running Android 5.0 or above, and currently there are no support-library implementations. The source files of this tutorial can be found on GitHub.

1. Creating a Vector Drawable

The main similarity between a VectorDrawable and a standard SVG image is that both are drawn out using a path value. While understanding how SVG paths are drawn is out of the scope of this article, official documentation can be found on the W3C website. For this article, you'll simply need to know that the path tag is where the drawing occurs. Let's take a look at the SVG file that draws out the following image:

Image of a CPU that will be drawn out in code

There are five major parts to this image:

  • a square for the CPU body made up of two arches
  • four groups of five lines that represent the CPU's wires

The following code draws this image out as an SVG:

While this may look a little overwhelming, you don't actually need to fully understand how everything is drawn out to implement a VectorDrawable in your code. However, it should be noted that I separated each of the five sections into their own unique block in the code for readability.

The top section consists of two arches to draw out the rounded square and the sections that follow represent the bottom, top, right, and left sets of lines respectively. To turn this SVG code into a VectorDrawable, you first need to define the vector object in XML. The following code is taken from the vector_drawable_cpu.xml file in the sample code for this article.

Next, you can add in the path data. The following code is broken up into five different path tags rather than one large path.

As you can see, each path section simply uses the pathData attribute for drawing. You can now include the VectorDrawable XML file as a drawable in a standard ImageView and it will scale to any size your app requires, without needing to use any Java code.

2. Animating Vector Drawables

Now that you know how to create images using only code, it's time to have a little fun and animate them. In the following animation, you'll notice that each of the groups of wires are pulsing towards and away from the CPU.

Example of animated VectorDrawables

To achieve this effect, you will need to wrap each section that you want to animate in a <group> tag. The updated version of vector_drawable_cpu.xml then looks like this:

Next, you will want to create animators for each animation type. In this case, there is one for each group of wires for a total of four. Below is an example of the top group's animation and you will also need one for the bottom, left, and right. Each of the animator XML files can be found in the sample code.

As you can see, the propertyName is set to translateY, which means the animation will move along the Y axis. The valueFrom and valueTo control the begin and end location. By setting repeatMode to reverse and repeatCount to infinite, the animation will loop forever as long as the VectorDrawable is visible. The duration of the animation is set to 250, which is the time in milliseconds.

To apply the animations to your drawable file, you will need to create a new animated-vector XML file to associate the animators with the VectorDrawable groups. The following code is used to create the animated_cpu.xml file.

When all of your XML are ready to go, you can use the animated_cpu.xml drawable in an ImageView to display it.

To start your animation, you will need to get an instance of the Animatable from the ImageView and call start.

After start has been called, the wires on the CPU image will start to move with very minimal Java code used.

3. Transforming Vector Drawables

A common use case for a VectorDrawable is transforming one image into another, such as the action bar icon that changes from a hamburger icon into an arrow. To do this, both the source and destination paths must follow an identical format for the number of elements. For this example we will define the left and right facing arrows seen above as strings.

Next, you will need to create an initial drawable for an arrow using the path for left_arrow. In the sample code, it is called vector_drawable_left_arrow.xml.

The main difference between the CPU animation and the transformation lies in the animator_left_right_arrow.xml file.

You'll notice the valueFrom and valueTo properties reference the path data for the left and right arrow, the valueType is set to pathType and propertyName is set to pathData. When these are set, the animator will know to change one set of path data to the other. When the animator is finished, you need to associate the VectorDrawable with the objectAnimator using a new animated-vector object.

Finally, you'll simply need to associate the animated drawable with an ImageView and start the animation in your Java code.

Conclusion

As you have seen, the VectorDrawable class is fairly straightforward to use and allows for a lot of customization to add simple animations. While the VectorDrawable class is currently only available for devices running Android 5.0 and above, they will be invaluable as more devices support Lollipop and future Android releases.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.