diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SingleEntryValueAnimator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SingleEntryValueAnimator.java new file mode 100644 index 00000000..b423505b --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SingleEntryValueAnimator.java @@ -0,0 +1,48 @@ +package nodomain.freeyourgadget.gadgetbridge.activities.charts; + +import android.animation.Animator; +import android.animation.ObjectAnimator; +import android.animation.ValueAnimator; + +import com.github.mikephil.charting.animation.ChartAnimator; +import com.github.mikephil.charting.data.Entry; + +public class SingleEntryValueAnimator extends ChartAnimator { + private final Entry entry; + private final ValueAnimator.AnimatorUpdateListener listener; + private float previousValue; + + public SingleEntryValueAnimator(Entry singleEntry, ValueAnimator.AnimatorUpdateListener listener) { + super(listener); + this.listener = listener; + entry = singleEntry; + } + + public void setEntryYValue(float value) { + this.previousValue = entry.getVal(); + entry.setVal(value); + } + + @Override + public void animateY(int durationMillis) { + // we start with the previous value and animate the change to the + // next value. + // as our animation values are not used as absolute values, but as factors, + // we have to calculate the proper factors in advance. The entry already has + // the new value, so we create a factor to calculate the old value from the + // new value. + + float startAnim; + float endAnim = 1f; + if (entry.getVal() == 0f) { + startAnim = 0f; + } else { + startAnim = previousValue / entry.getVal(); + } + + ObjectAnimator animatorY = ObjectAnimator.ofFloat(this, "phaseY", startAnim, endAnim); + animatorY.setDuration(durationMillis); + animatorY.addUpdateListener(listener); + animatorY.start(); + } +}