1 package com.github.mikephil.charting.animation; 2 3 import android.animation.ObjectAnimator; 4 import android.animation.ValueAnimator.AnimatorUpdateListener; 5 import androidx.annotation.RequiresApi; 6 7 import com.github.mikephil.charting.animation.Easing.EasingFunction; 8 9 /** 10 * Object responsible for all animations in the Chart. Animations require API level 11. 11 * 12 * @author Philipp Jahoda 13 * @author Mick Ashton 14 */ 15 public class ChartAnimator { 16 17 /** object that is updated upon animation update */ 18 private AnimatorUpdateListener mListener; 19 20 /** The phase of drawn values on the y-axis. 0 - 1 */ 21 @SuppressWarnings("WeakerAccess") 22 protected float mPhaseY = 1f; 23 24 /** The phase of drawn values on the x-axis. 0 - 1 */ 25 @SuppressWarnings("WeakerAccess") 26 protected float mPhaseX = 1f; 27 ChartAnimator()28 public ChartAnimator() { } 29 30 @RequiresApi(11) ChartAnimator(AnimatorUpdateListener listener)31 public ChartAnimator(AnimatorUpdateListener listener) { 32 mListener = listener; 33 } 34 35 @RequiresApi(11) xAnimator(int duration, EasingFunction easing)36 private ObjectAnimator xAnimator(int duration, EasingFunction easing) { 37 38 ObjectAnimator animatorX = ObjectAnimator.ofFloat(this, "phaseX", 0f, 1f); 39 animatorX.setInterpolator(easing); 40 animatorX.setDuration(duration); 41 42 return animatorX; 43 } 44 45 @RequiresApi(11) yAnimator(int duration, EasingFunction easing)46 private ObjectAnimator yAnimator(int duration, EasingFunction easing) { 47 48 ObjectAnimator animatorY = ObjectAnimator.ofFloat(this, "phaseY", 0f, 1f); 49 animatorY.setInterpolator(easing); 50 animatorY.setDuration(duration); 51 52 return animatorY; 53 } 54 55 /** 56 * Animates values along the X axis, in a linear fashion. 57 * 58 * @param durationMillis animation duration 59 */ 60 @RequiresApi(11) animateX(int durationMillis)61 public void animateX(int durationMillis) { 62 animateX(durationMillis, Easing.Linear); 63 } 64 65 /** 66 * Animates values along the X axis. 67 * 68 * @param durationMillis animation duration 69 * @param easing EasingFunction 70 */ 71 @RequiresApi(11) animateX(int durationMillis, EasingFunction easing)72 public void animateX(int durationMillis, EasingFunction easing) { 73 74 ObjectAnimator animatorX = xAnimator(durationMillis, easing); 75 animatorX.addUpdateListener(mListener); 76 animatorX.start(); 77 } 78 79 /** 80 * Animates values along both the X and Y axes, in a linear fashion. 81 * 82 * @param durationMillisX animation duration along the X axis 83 * @param durationMillisY animation duration along the Y axis 84 */ 85 @RequiresApi(11) animateXY(int durationMillisX, int durationMillisY)86 public void animateXY(int durationMillisX, int durationMillisY) { 87 animateXY(durationMillisX, durationMillisY, Easing.Linear, Easing.Linear); 88 } 89 90 /** 91 * Animates values along both the X and Y axes. 92 * 93 * @param durationMillisX animation duration along the X axis 94 * @param durationMillisY animation duration along the Y axis 95 * @param easing EasingFunction for both axes 96 */ 97 @RequiresApi(11) animateXY(int durationMillisX, int durationMillisY, EasingFunction easing)98 public void animateXY(int durationMillisX, int durationMillisY, EasingFunction easing) { 99 100 ObjectAnimator xAnimator = xAnimator(durationMillisX, easing); 101 ObjectAnimator yAnimator = yAnimator(durationMillisY, easing); 102 103 if (durationMillisX > durationMillisY) { 104 xAnimator.addUpdateListener(mListener); 105 } else { 106 yAnimator.addUpdateListener(mListener); 107 } 108 109 xAnimator.start(); 110 yAnimator.start(); 111 } 112 113 /** 114 * Animates values along both the X and Y axes. 115 * 116 * @param durationMillisX animation duration along the X axis 117 * @param durationMillisY animation duration along the Y axis 118 * @param easingX EasingFunction for the X axis 119 * @param easingY EasingFunction for the Y axis 120 */ 121 @RequiresApi(11) animateXY(int durationMillisX, int durationMillisY, EasingFunction easingX, EasingFunction easingY)122 public void animateXY(int durationMillisX, int durationMillisY, EasingFunction easingX, 123 EasingFunction easingY) { 124 125 ObjectAnimator xAnimator = xAnimator(durationMillisX, easingX); 126 ObjectAnimator yAnimator = yAnimator(durationMillisY, easingY); 127 128 if (durationMillisX > durationMillisY) { 129 xAnimator.addUpdateListener(mListener); 130 } else { 131 yAnimator.addUpdateListener(mListener); 132 } 133 134 xAnimator.start(); 135 yAnimator.start(); 136 } 137 138 /** 139 * Animates values along the Y axis, in a linear fashion. 140 * 141 * @param durationMillis animation duration 142 */ 143 @RequiresApi(11) animateY(int durationMillis)144 public void animateY(int durationMillis) { 145 animateY(durationMillis, Easing.Linear); 146 } 147 148 /** 149 * Animates values along the Y axis. 150 * 151 * @param durationMillis animation duration 152 * @param easing EasingFunction 153 */ 154 @RequiresApi(11) animateY(int durationMillis, EasingFunction easing)155 public void animateY(int durationMillis, EasingFunction easing) { 156 157 ObjectAnimator animatorY = yAnimator(durationMillis, easing); 158 animatorY.addUpdateListener(mListener); 159 animatorY.start(); 160 } 161 162 /** 163 * Gets the Y axis phase of the animation. 164 * 165 * @return float value of {@link #mPhaseY} 166 */ getPhaseY()167 public float getPhaseY() { 168 return mPhaseY; 169 } 170 171 /** 172 * Sets the Y axis phase of the animation. 173 * 174 * @param phase float value between 0 - 1 175 */ setPhaseY(float phase)176 public void setPhaseY(float phase) { 177 if (phase > 1f) { 178 phase = 1f; 179 } else if (phase < 0f) { 180 phase = 0f; 181 } 182 mPhaseY = phase; 183 } 184 185 /** 186 * Gets the X axis phase of the animation. 187 * 188 * @return float value of {@link #mPhaseX} 189 */ getPhaseX()190 public float getPhaseX() { 191 return mPhaseX; 192 } 193 194 /** 195 * Sets the X axis phase of the animation. 196 * 197 * @param phase float value between 0 - 1 198 */ setPhaseX(float phase)199 public void setPhaseX(float phase) { 200 if (phase > 1f) { 201 phase = 1f; 202 } else if (phase < 0f) { 203 phase = 0f; 204 } 205 mPhaseX = phase; 206 } 207 } 208