2012-02-26 44 views
0

在我的Android应用程序中,我使用跟随功能。但我发现当time大于16777215(0xFFFFFF)时,计算需要很多时间,并且每2秒增加一个而不是1.计算耗尽时间

有没有一种方法可以优化下面的代码,以便处理大于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

尝试使用double或long来代替float来加速计算。

如果您保留日期,小时,分钟和秒变量与时间相同的类型,您也可以摆脱演员阵容。