在ValueAnimator中使用Float值会导致异常 [英] Using Float value in ValueAnimator causes Exception

查看:360
本文介绍了在ValueAnimator中使用Float值会导致异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作从零到第n个数字的递增行为的动画. ValueAnimator适用于整数值,但我尝试使用浮点值进行此操作,并使用ClassCastException崩溃了.

I am trying to animate the incrementing behaviour of a number from zero to a nth number. ValueAnimator works for an integer value but I attempted to this with a float value and it crashes with a ClassCastException.

例外:

java.lang.ClassCastException:无法将java.lang.Float强制转换为java.lang.Integer

java.lang.ClassCastException: java.lang.Float cannot be cast to 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();

要使此值适用于浮点数,我需要做些什么更改?谢谢!

What changes do I need to do to make this work for float values? Thanks!

推荐答案

在我看来,您是在滥用ValueAnimator.

It seems to me you are misusing the ValueAnimator.

使用Value Animator更为简单的方法是使用ValueAnimator.ofFloat(float... values),这将自动为您设置Evaluator.在您的代码中,您将浮点值提供给Integer Evaluator,从而导致强制转换异常.

A much easier approach to using the Value Animator would be to use ValueAnimator.ofFloat(float... values) this will automatically set the Evaluator for you. In your code you are supplying float values to an Integer Evaluator, causing your cast exception.

ValueAnimator animator = ValueAnimator.ofFloat(0f, 100.2f);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                tvTotalAmount.setText(String.valueOf((float)animation.getAnimatedValue()));
            }
        });
animator.setDuration(1500);
animator.start();

此代码应适合您的情况.我将更新侦听器更改为将文本设置为任何animationValue,以便您的ValueAnimator实际执行某些操作.我看不出每次动画器更新时都将文本设置为"100.2"的意义,因为用户可能看不到任何变化,尽管无论如何您可能还有其他针对UpdateListener的计划.

This code should work for your situation. I changed the update listener to set the text to whatever the animatedValue is so that your ValueAnimator actually does something. I do not see much point to setting the text to "100.2" every time the animator updates as the user would see no change, although you probably had other plans for the UpdateListener anyhow.

或者,您可以修复评估程序来评估Float而不是Integer.

Alternatively, you could fix your Evaluator to evaluate Floats instead of Integers.

animator.setEvaluator(new TypeEvaluator<Float>() {
    @Override
    public Float evaluate(float fraction, Float startValue, Float endValue) {
        return (startValue + (endValue - startValue) * fraction);
    }
});

截断动画值

如果您想将增加的值限制为仅小数点后一位,我建议您这样做:

If you want to limit the increasing value to only 1 decimal place, I would suggest doing this:

DecimalFormat df = new DecimalFormat("###.#");
df.setRoundingMode(RoundingMode.DOWN); //DOWN if you dont want number rounded, UP if you do want it rounded
float oneDecimal = df.format((float)animation.getAnimatedValue());

将此代码放在您的onAnimationUpdate侦听器中,然后您可以将动画值的截断值接收到小数点后一位.

Place this code within your onAnimationUpdate listener and you can then receive a truncated value of the animated value to one decimal place.

这篇关于在ValueAnimator中使用Float值会导致异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆