我在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
,则第一个字母的大小应为0float size = 1 / (1 / length * (i+1)) * interpolatedTime;
float size = 1 / (1 / 2 * (0 + 1)) * 0,0;
但是我将错误除以零
答案 0 :(得分:1)
您正在使用1 / 2
执行整数除法,它在Java中生成另一个整数0
,导致您除以零错误。
将1
指定为float
文字1.0f
,从一开始就强制使用浮点运算。
float size = 1 / (1.0f / length * (i+1)) * interpolatedTime;