J'essaie d'animer le comportement d'incrémentation d'un nombre de zéro à un nième nombre. ValueAnimator fonctionne pour une valeur entière mais j'ai essayé de le faire avec une valeur flottante et il se bloque avec un ClassCastException
.L'utilisation de la valeur Float dans ValueAnimator provoque l'exception
Exception:
java.lang.ClassCastException: java.lang.Float ne peut pas être jeté à java.lang.Integer
code:
ValueAnimator animator = new ValueAnimator();
animator.setObjectValues(0, 100.2f);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
tvTotalAmount.setText("100.2");
}
});
animator.setEvaluator(new TypeEvaluator<Integer>() {
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
return Math.round(startValue + (endValue - startValue) * fraction);
}
});
animator.setDuration(1500);
animator.start();
Quels changements dois-je faire pour que cela fonctionne pour les valeurs flottantes? Merci!
Avez-vous essayé d'utiliser un FloatEvaluator au lieu d'un TypeEvaluator? https://developer.android.com/reference/android/animation/FloatEvaluator.html – khriskooper
animator.setEvaluator() Utilisez Float au lieu de Integer. –