From 0395977fdec07c16280e1dd69cd850bf7c609c85 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sun, 27 Sep 2015 00:10:33 +0200 Subject: [PATCH] Some work for properly animating our single (value-changing) entry --- .../charts/SingleEntryValueAnimator.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SingleEntryValueAnimator.java 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 000000000..b423505bb --- /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(); + } +}