我正在尝试动画从零到第n个数字的数字的增量行为。 ValueAnimator适用于整数值,但我试图用浮点值来解决这个问题,并使用ClassCastException
崩溃。在ValueAnimator中使用浮点值会导致异常
例外:
java.lang.ClassCastException:java.lang.Float中不能转换为java.lang.Integer中
代码:
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();
我需要做些什么才能使这项工作适用于浮点值?谢谢!
您是否尝试过使用FloatEvaluator而不是TypeEvaluator? https://developer.android.com/reference/android/animation/FloatEvaluator.html – khriskooper
animator.setEvaluator()使用Float而不是Integer。 –