How to make Check/Tick and Cross animations in Android

AnimatedVectorDrawable class was introduced in API 21 and is used to animate Vector Drawables beautifully and easily. Using AnimatedVectorDrawable, one can:
- Rotate, Scale, Translate VectorDrawables
 - Animate the VectorDrawable such as fill color etc.
 - Draw paths and do Path Morphing
 
An AnimatedVectorDrawable element has a VectorDrawable attribute, and one or more target element(s). The target element can specify its target by android:name attribute, and link the target with the proper ObjectAnimator or AnimatorSet by android:animation attribute.
Approach to draw Tick Cross animation:
- Create a new tick_cross.xml file in values directory and add the following vector drawable path data and path commands:
tick_cross.xml
<?xmlversion="1.0"encoding="UTF-8"?><resources>ÂÂÂ Â Â<!-- geometry -->Â Â Â<integername="viewport_width">24</integer>Â Â Â<integername="viewport_height">24</integer>Â Â Â<integername="viewport_center_x">12</integer>Â Â Â<integername="viewport_center_y">12</integer>Â Â Â<stringname="path_tick">M4.8, 13.4 L9,ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â17.6 M10.4,ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â16.2 L19.6, 7Â Â Â</string>Â Â Â<stringname="path_cross">M6.4, 6.4 L17.6,ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â17.6 M6.4,ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â17.6 L17.6, 6.4Â Â Â</string>Â Â Â<integername="stroke_width">2</integer>ÂÂÂ Â Â<!-- names -->Â Â Â<stringname="tick">tick</string>Â Â Â<stringname="cross">cross</string>Â Â Â<stringname="groupTickCross">groupTickCross</string>ÂÂÂ Â Â<!-- drawing -->Â Â Â<colorname="stroke_color">#999</color></resources> - Now create a new Android Resource Directory. Right-click on res folder and select Android Resource Directory. Make sure to select resource type as animator.
Animators can change the physical properties of the objects. This means that if you move a View to a new location, the touch coordinates will be mapped at the new location without any other intervention. - Now create a new cross_to_tick.xml file in the animator directory. In this file, we mainly define the duration and animation type to the cross_to_tick.xml. This file is responsible for the conversion of the cross to tick when the user clicks on the cross icon.
cross_to_tick.xml
<?xmlversion="1.0"encoding="utf-8"?><objectAnimator   Âandroid:propertyName="pathData"   Âandroid:valueFrom="@string/path_cross"   Âandroid:valueTo="@string/path_tick"   Âandroid:duration="500"   Âandroid:interpolator="@android:interpolator/fast_out_slow_in"   Âandroid:valueType="pathType"/> - Now create a new tick_to_cross.xml file in animator directory. In this file, we mainly define the duration and animation type to the tick_to_cross.xml. This file is responsible for the conversion of the tick to cross when the user clicks on the tick icon.
tick_to_cross.xml
<?xmlversion="1.0"encoding="utf-8"?><objectAnimator   Âandroid:propertyName="pathData"   Âandroid:valueFrom="@string/path_tick"   Âandroid:valueTo="@string/path_cross"   Âandroid:duration="500"   Âandroid:interpolator="@android:interpolator/fast_out_slow_in"   Âandroid:valueType="pathType"/> - Now create a new rotate_cross_to_tick.xml file in animator directory. In this file, we mainly define the duration and animation type to the tick_to_cross.xml. This file is responsible for the rotation when the user clicks on the cross icon.
rotate_cross_to_tick.xml
<?xmlversion="1.0"encoding="utf-8"?><objectAnimator   Âandroid:propertyName="rotation"   Âandroid:valueFrom="-180"   Âandroid:valueTo="0"   Âandroid:duration="500"   Âandroid:interpolator="@android:interpolator/fast_out_slow_in"/> - Now create a new rotate_tick_to_cross.xml file in animator directory. In this file, we mainly define the duration and animation type to the tick_to_cross.xml. This file is responsible for the rotation when the user clicks on the tick icon.
rotate_cross_to_tick.xml
<?xmlversion="1.0"encoding="utf-8"?><objectAnimator   Âandroid:propertyName="rotation"   Âandroid:valueFrom="0"   Âandroid:valueTo="180"   Âandroid:duration="500"   Âandroid:interpolator="@android:interpolator/fast_out_slow_in"/> - Now create a new avd_cross_to_tick.xml file in the drawable directory and the following code.
avd_cross_to_tick.xml
<?xmlversion="1.0"encoding="utf-8"?><animated-vector   Âandroid:drawable="@drawable/ic_cross">   Â<target       Âandroid:name="@string/cross"       Âandroid:animation="@animator/cross_to_tick"/>   Â<target       Âandroid:name="@string/groupTickCross"       Âandroid:animation="@animator/rotate_cross_to_tick"/>ÂÂ</animated-vector> - Now create a new avd_tick_to_cross.xml file in the drawable directory and the following code.
avd_tick_to_cross.xml
<?xmlversion="1.0"encoding="utf-8"?><animated-vector   Âandroid:drawable="@drawable/ic_tick">   Â<target       Âandroid:name="@string/tick"       Âandroid:animation="@animator/tick_to_cross"/>   Â<target       Âandroid:name="@string/groupTickCross"       Âandroid:animation="@animator/rotate_tick_to_cross"/>ÂÂ</animated-vector> - In this step, we will define a static drawable object for vector graphics. Now create a new ic_tick.xml file in drawable directory and the following code.
ic_tick.xml
<?xmlversion="1.0"encoding="utf-8"?>       Âandroid:width="120dp"       Âandroid:height="120dp"       Âandroid:viewportWidth="@integer/viewport_width"       Âandroid:viewportHeight="@integer/viewport_height">   Â<group       Âandroid:name="@string/groupTickCross"       Âandroid:pivotX="@integer/viewport_center_x"       Âandroid:pivotY="@integer/viewport_center_y">       Â<path           Âandroid:name="@string/tick"           Âandroid:pathData="@string/path_tick"           Âandroid:strokeWidth="@integer/stroke_width"           Âandroid:strokeLineCap="square"           Âandroid:strokeColor="@color/stroke_color"/>   Â</group></vector> - In this step, we will define a static drawable object for vector graphics. Now create a new ic_cross.xml file in drawable directory and the following code.
ic_cross.xml
<?xmlversion="1.0"encoding="utf-8"?>       Âandroid:width="120dp"       Âandroid:height="120dp"       Âandroid:viewportWidth="@integer/viewport_width"       Âandroid:viewportHeight="@integer/viewport_height">   Â<group       Âandroid:name="@string/groupTickCross"       Âandroid:pivotX="@integer/viewport_center_x"       Âandroid:pivotY="@integer/viewport_center_y">       Â<path           Âandroid:name="@string/cross"           Âandroid:pathData="@string/path_cross"           Âandroid:strokeWidth="@integer/stroke_width"           Âandroid:strokeLineCap="square"           Âandroid:strokeColor="@color/stroke_color"/>   Â</group>ÂÂ</vector> - Now add the following code in the activity_main.xml file.
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><FrameLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent"   Âandroid:onClick="animate"   Âtools:context=".MainActivity">   Â<ImageView       Âandroid:id="@+id/tick_cross"       Âandroid:layout_width="wrap_content"       Âandroid:layout_height="wrap_content"       Âandroid:layout_gravity="center"       Âandroid:src="@drawable/ic_tick"/>ÂÂ</FrameLayout> - Now add the following code in the MainActivity.xml file.
MainActivity.xml
packageorg.zambiatek.tickcross;ÂÂimportandroidx.appcompat.app.AppCompatActivity;ÂÂimportandroid.graphics.drawable.AnimatedVectorDrawable;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.ImageView;ÂÂpublicclassMainActivityextendsAppCompatActivity {   ÂprivateImageView tickCross;   ÂprivateAnimatedVectorDrawable tickToCross;   ÂprivateAnimatedVectorDrawable crossToTick;   Âprivatebooleantick =true;   Â@Override   ÂprotectedvoidonCreate(Bundle savedInstanceState)   Â{       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       ÂtickCross = findViewById(R.id.tick_cross);       ÂtickToCross = (AnimatedVectorDrawable)           ÂgetDrawable(               ÂR.drawable.avd_tick_to_cross);       ÂcrossToTick = (AnimatedVectorDrawable)           ÂgetDrawable(               ÂR.drawable.avd_cross_to_tick);   Â}   Â// This method help to animate our view.   Âpublicvoidanimate(View view)   Â{       ÂAnimatedVectorDrawable drawable           Â= tick                 Â? tickToCross                 Â: crossToTick;       ÂtickCross.setImageDrawable(drawable);       Âdrawable.start();       Âtick = !tick;   Â}} 
In the next two steps we will be creating AnimatedVectorDrawable for both cross_to_tick and tick_to_cross.
Output:
				
					


