2015-09-27 00:10:33 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
|
|
|
|
|
|
|
|
import android.animation.ObjectAnimator;
|
|
|
|
import android.animation.ValueAnimator;
|
|
|
|
|
|
|
|
import com.github.mikephil.charting.animation.ChartAnimator;
|
|
|
|
import com.github.mikephil.charting.data.Entry;
|
|
|
|
|
2015-10-01 22:36:33 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-09-27 00:10:33 +02:00
|
|
|
public class SingleEntryValueAnimator extends ChartAnimator {
|
2015-10-01 22:36:33 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(SingleEntryValueAnimator.class);
|
|
|
|
|
2015-09-27 00:10:33 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-10-02 23:38:07 +02:00
|
|
|
// LOG.debug("anim factors: " + startAnim + ", " + endAnim);
|
2015-10-01 22:36:33 +02:00
|
|
|
|
2015-09-27 00:10:33 +02:00
|
|
|
ObjectAnimator animatorY = ObjectAnimator.ofFloat(this, "phaseY", startAnim, endAnim);
|
|
|
|
animatorY.setDuration(durationMillis);
|
|
|
|
animatorY.addUpdateListener(listener);
|
|
|
|
animatorY.start();
|
|
|
|
}
|
|
|
|
}
|