1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 18:15:49 +01:00

Some work for properly animating our single (value-changing) entry

This commit is contained in:
cpfeiffer 2015-09-27 00:10:33 +02:00
parent 518b1ee6f4
commit 0395977fde

View File

@ -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();
}
}