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