applyTransformation除以零错误

时间:2014-05-15 19:31:13

标签: java android

我在androids动画类的方法applyTransformation中将误差除以零:

05-15 21:13:59.330: E/AndroidRuntime(16997): java.lang.ArithmeticException: divide by zero
05-15 21:13:59.330: E/AndroidRuntime(16997):    at com.special.ResideMenuDemo.FontResize.applyTransformation(FontResize.java:34)

那是我的applyTransformation方法:

@Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);

        SpannableStringBuilder spanTxt = new SpannableStringBuilder("He");

        int length = spanTxt.length();

        for (int i = 0; i < length; i++) {

            /*Log.e("Log", "length : " + length + "\r\n" + 
                    "i : " + i + "\r\n" + 
                    "interpolatedTime : " + interpolatedTime);*/

            float size = 1 / (1 / length * (i+1)) * interpolatedTime;

            if(size >= 1)
                continue;

            spanTxt.setSpan(new RelativeSizeSpan( size ), i, i+1, 0 );

        }

        text.setText(spanTxt);

    }

如果interpolatedTime为0

,则第一个字母的大小应为0
float size = 1 / (1 / length * (i+1)) * interpolatedTime;
float size = 1 / (1 / 2 * (0 + 1)) * 0,0;

但是我将错误除以零

1 个答案:

答案 0 :(得分:1)

您正在使用1 / 2执行整数除法,它在Java中生成另一个整数0,导致您除以零错误。

1指定为float文字1.0f,从一开始就强制使用浮点运算。

float size = 1 / (1.0f / length * (i+1)) * interpolatedTime;