2014-05-15 36 views
0

零差錯,我得到的方法applyTransformation零誤差鴻溝在機器人動畫類:除以上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); 

    } 

的第一個字母的大小應該是0,如果interpolatedTime是0

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

但我得到的錯誤div ide by zero

回答

1

您正在執行整數除法與1/2,在Java中導致另一個整數,0,導致您的除以零錯誤。

指定1float文字,1.0f,從頭開始強制執行浮點算術。

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