2012-02-26 14 views
0

私のAndroidアプリケーションで私はfollow関数を使用しています。しかし、timeが16777215(0xFFFFFF)より大きい場合、計算に多くの時間がかかり、1ではなく2秒ごとにインクリメントされることがわかりました。時間外の計算

以下のコードを最適化して、 0xFFFFFF時間ですか?

/** 
* Sets the time wheels to the right time. 
* @param time in seconds 
* @param animation for enableing and disabling animation 
*/ 
private void setTime(float time, boolean animation) {   
    int days; 
    int hours; 
    int mins; 
    int secs; 

    days = (int) (time/(3600*24)); 
    hours = (int)(time/3600); 
    time = time % 3600; 
    mins = (int) (time/60); 
    time = time % 60; 
    secs = (int) time; 

    if(days >= 1){ 
     getWheel(R.id.time4).setCurrentItem(days, animation); 
    } else { 
     getWheel(R.id.time4).setCurrentItem(0, animation); 
    } 

    if(hours >= 1){ 
     getWheel(R.id.time3).setCurrentItem(hours, animation); 
    } else { 
     getWheel(R.id.time3).setCurrentItem(0, animation); 
    } 

    if(mins >= 1){ 
     getWheel(R.id.time2).setCurrentItem(mins, animation); 
    } else { 
     getWheel(R.id.time2).setCurrentItem(0, animation); 
    } 

    if(secs >= 1){ 
     getWheel(R.id.time1).setCurrentItem(secs, animation); 
    } else { 
     getWheel(R.id.time1).setCurrentItem(0, animation); 
    } 
} 

答えて

1

floatの代わりにdouble、またはlongを使用して計算を高速化してみてください。

日、時、分、および秒の変数を、キャストを取り除く時間と同じタイプにすると、