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 Heart fill animation:
- Create a new heart.xml file in values directory and add the following vector drawable path data and path commands:
heart.xml
<?xmlversion="1.0"encoding="utf-8"?><resources>   Â<!-- geometry -->   Â<stringname="heart_empty_path">       ÂM32.95, 19 C31.036, 19 29.199, 19.8828338 28,       Â21.2724796 C26.801, 19.8828338 24.964,       Â19 23.05, 19 C19.6565, 19 17, 21.6321526 17,       Â24.9945504 C17, 29.1089918 20.74, 32.4713896       Â26.405, 37.5667575 L28, 39 L29.595, 37.5667575 C35.26,       Â32.4713896 39, 29.1089918 39, 24.9945504 C39,       Â21.6321526 36.3435, 19 32.95, 19 L32.95,       Â19 Z M28.1155, 35.9536785 L28, 36.0572207 L27.8845,       Â35.9536785 C22.654, 31.2506812 19.2,       Â28.1444142 19.2, 24.9945504 C19.2, 22.8201635 20.8555,       Â21.1798365 23.05, 21.1798365 C24.744,       Â21.1798365 26.394, 22.2643052 26.9715, 23.7520436       ÂL29.023, 23.7520436 C29.606, 22.2643052 31.256,       Â21.1798365 32.95, 21.1798365 C35.1445,       Â21.1798365 36.8, 22.8201635 36.8,       Â24.9945504 C36.8, 28.1444142 33.346,       Â31.2506812 28.1155, 35.9536785 L28.1155,       Â35.9536785 Z   Â</string>   Â<stringname="heart_full_path">       ÂM28, 39 L26.405, 37.5667575 C20.74,       Â32.4713896 17, 29.1089918 17,       Â24.9945504 C17, 21.6321526 19.6565,       Â19 23.05, 19 C24.964, 19 26.801, 19.8828338 28,       Â21.2724796 C29.199, 19.8828338 31.036, 19 32.95,       Â19 C36.3435, 19 39, 21.6321526 39, 24.9945504 C39,       Â29.1089918 35.26, 32.4713896 29.595,       Â37.5667575 L28, 39 L28, 39 Z   Â</string>   Â<stringname="heart_clip_hidden">       ÂM18 37 L38 37 L38 37 L18 37 Z   Â</string>   Â<stringname="heart_clip_shown">       ÂM0 0 L56 0 L56 56 L0 56 Z   Â</string></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 empty_heart.xml file in the animator directory. In this file, we mainly define the duration and animation type to the empty_heart.xml. This file is responsible for the animation when the user clicks on the filled heart icon.
empty_heart.xml
<?xmlversion="1.0"encoding="utf-8"?><objectAnimator   Âandroid:propertyName="pathData"   Âandroid:valueFrom="@string/heart_clip_shown"   Âandroid:valueTo="@string/heart_clip_hidden"   Âandroid:duration="600"   Âandroid:interpolator="@android:interpolator/fast_out_slow_in"   Âandroid:valueType="pathType"/> - Now create a new fill_heart.xml file in animator directory. In this file, we mainly define the duration and animation type to the empty_heart.xml. This file is responsible for the animation when the user clicks on the empty heart icon.
fill_heart.xml
<?xmlversion="1.0"encoding="utf-8"?><objectAnimator   Âandroid:propertyName="pathData"   Âandroid:valueFrom="@string/heart_clip_hidden"   Âandroid:valueTo="@string/heart_clip_shown"   Âandroid:duration="800"   Âandroid:interpolator="@android:interpolator/fast_out_slow_in"   Âandroid:valueType="pathType"/> - Now create a new avd_heart_empty.xml file in the drawable directory and the following code.
avd_heart_empty.xml
<?xmlversion="1.0"encoding="utf-8"?><animated-vector   Âandroid:drawable="@drawable/ic_heart">   Â<target       Âandroid:name="clip"       Âandroid:animation="@animator/empty_heart"/></animated-vector> - Now create a new avd_heart_fill.xml file in drawable directory and the following code.
avd_heart_fill.xml
<?xmlversion="1.0"encoding="utf-8"?><animated-vector   Âandroid:drawable="@drawable/ic_heart">   Â<target       Âandroid:name="clip"       Âandroid:animation="@animator/fill_heart"/>ÂÂ</animated-vector> - In this step, we will define a static drawable object for vector graphics. Now create a new ic_heart.xml file in drawable directory and the following code.
ic_heart.xml
<?xmlversion="1.0"encoding="utf-8"?><vector   Âandroid:width="280dp"   Âandroid:height="280dp"   Âandroid:viewportWidth="56"   Âandroid:viewportHeight="56">   Â<path       Âandroid:name="empty"       Âandroid:pathData="@string/heart_empty_path"       Âandroid:fillColor="#219806"/>   Â<clip-path       Âandroid:name="clip"       Âandroid:pathData="@string/heart_clip_hidden"/>   Â<path       Âandroid:name="full"       Âandroid:pathData="@string/heart_full_path"       Âandroid:fillColor="#1A7A04"/>ÂÂ</vector> - Now add the following code in the MainActivity.java file.
MainActivity.java
packageorg.neveropen.fillheart;ÂÂimportandroid.app.Activity;importandroid.graphics.drawable.AnimatedVectorDrawable;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.ImageView;ÂÂpublicclassMainActivityextendsActivity {   ÂprivateImageView imageView;   ÂprivateAnimatedVectorDrawable emptyHeart;   ÂprivateAnimatedVectorDrawable fillHeart;   Âprivatebooleanfull =false;   Â@Override   ÂprotectedvoidonCreate(Bundle savedInstanceState)   Â{       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       ÂimageView = findViewById(R.id.image_view);       ÂemptyHeart           Â= (AnimatedVectorDrawable)               ÂgetDrawable(                   ÂR.drawable.avd_heart_empty);       ÂfillHeart           Â= (AnimatedVectorDrawable)               ÂgetDrawable(                   ÂR.drawable.avd_heart_fill);   Â}   Â// This method help to animate our view.   Âpublicvoidanimate(View view)   Â{       ÂAnimatedVectorDrawable drawable           Â= full                 Â? emptyHeart                 Â: fillHeart;       ÂimageView.setImageDrawable(drawable);       Âdrawable.start();       Âfull = !full;   Â}} - Now compile and run the Android app.
In the next two steps, we will be creating AnimatedVectorDrawable for both empty heart and filled heart.
Output:
Reference:
