Creating Animated Vector Drawable for Android

Android animated vector drawable file contains all the data regarding actual vector as well as any animations applied on the parts of that vector. No extra programming or code is required to be added in code files. Format for animated vector drawable is as follows.

Sample Animated Vector Drawable File for Android

Top level element

Top level element is animated-vector which should be defined as follows.

Second Level Elements

Second level elements are :
  1. one drawable containing many paths and their groups
  2. Multiple animation targets.

Drawable Syntax

Syntax for drawable component is as follows. Element <aapt:attr> defines that it is a drawable. Actual drawable is encaptulated inside <vector> element. Multiple paths are present in <vector> element. Multiple paths can be grouped together in <group> element.

Target Syntax

<target> element is used to define animation for various named groups or paths inside vector above. Details for various types of animations are provided in following sections. <target> element has “name” property where you specify name given to a path or group of paths inside <vector> element. You can specify various animation properties like animation name, duration, property to be animated, values from and to as well as any repeat counts etc.

Defining various types of animations inside <target Element.

<ObjectAnimator> element is used to define animations. Various properties that you can define in ObjectAnimator are as follows.
  1. propertyName
  2. duration : In Milliseconds
  3. valueFrom: as per property type.
  4. valueTo: as per property type.

android:repeatCount

RepeatCount can be “infinite” or some positive value.
infinite infinite animation repetitions
positive value like 2, 5. 10 etc. For fixed number of animation repetitions

android:valueType

ValueType is a type of value for the property to be animated. It can be a float value, integer value or color value.
floatType Used for rotations, translations, fillAlpha
colorType Used for fillColor, strokeColor
pathType Used for path morphing, path animation

android:repeatMode

Repeat mode is the way animation is repeated. There are three possible values for repeat mode.
infinite This value used used to repeat the animation indefinitely.
restart When the animation reaches the end and repeatCount is infinite or a positive value, the animation restarts from the beginning.
reverse When the animation reaches the end and repeatCount is INFINITE or a positive value, the animation reverses direction on every iteration.
Rotation Animation : For Group We can rotate a group of paths by providing fromValue and toValue, along with duration. We can also specify repeatMode and repeatCount.

Path Data Animation

Provide propertyName as “pathData”. Set path valuesFrom and valuTo values. Set valueType to “pathType”.

Fill Alpha Animation

For fill alpha animation, use property name as fillAlpha. Set duration as required. valueFrom and valueTo property must be set to floaType from 0 to 1.

Fill Color Animation

Use fillColor property name for fill color animation. provide fill color valueFrom and valueTo with valueType as “colorType”

Stroke Color Animation

Provide strokeColor property name for stroke color animation. Provide stroke color valueFrom and valueTo with valueType as “colorType”

Scale along X Axis

You can scale a path along X axis using scaleX property Name. Set valueFrom and valueTo as flotType.

Scale along Y Axis

You can scale a path along Y axis using scaleY property Name. Set valueFrom and valueTo as flotType.

Translate Along X Axis

You can move object along X axis using translateX property Name. Set valueFrom and valueTo as in pixels in as per ViewPort space co-ordinates. Original location value is 0. You can use it to make object appear from outside to its final location i.e. 0

Translate Along Y Axis

You can move object along Y axis using translateY property Name. Set valueFrom and valueTo as in pixels in as per ViewPort space co-ordinates. Original location value is 0. You can use it to make object appear from outside to its final location i.e. 0