2012-03-23 92 views
0

我試圖實現一種速度計。我得到關於每分鐘回合的信息,通過藍牙提升和加載引擎,我嘗試在屏幕上顯示它們,巫婆應該指向正確的方向。我試圖使用旋轉動畫evry時間我得到數據(10-100ms)來設置箭頭。但是這讓我的用戶感覺非常緩慢。 500ms以在按鈕上作出反應。有人知道如何使它更好地工作嗎?旋轉圖像使UI慢

源代碼:

public void setTacho() 
{ 


//rotate Tachonadel 
Rpmcurrentdegree=Rpmcurrentdegree+Rpmdegree; 
Rpmdegree=((rpms-lastrpm)*RPMtoDegree); 

RpmAnim=new RotateAnimation((float)Rpmcurrentdegree, (float)Rpmdegree,  ivNadel.getWidth()/2, ivNadel.getHeight()/2); 
RpmAnim.setFillEnabled(true); 
RpmAnim.setFillAfter(true); 
ivNadel.setAnimation(RpmAnim); 
RpmAnim.start(); 


//rotate Boostbalken 
currentBoostDegree=currentBoostDegree+BoostDegree; 
BoostDegree=(boost-lastBoost)*BOOSTtoDegree; 

//rotate Loadbalken 
currentLoadDegree=currentLoadDegree+LoadDegree; 
LoadDegree=(load-lastLoad)*LOADtoDegree; 


BoostAnim=new RotateAnimation((float)-currentBoostDegree, (float)-BoostDegree,  ivBoost.getWidth()/2, ivBoost.getHeight()/2); 
BoostAnim.setFillEnabled(true); 
BoostAnim.setFillAfter(true); 
ivBoost.setAnimation(BoostAnim); 
BoostAnim.start(); 

LoadAnim=new RotateAnimation((float)currentLoadDegree, (float)LoadDegree,  ivLoad.getWidth()/2, ivLoad.getHeight()/2); 
LoadAnim.setFillEnabled(true); 
LoadAnim.setFillAfter(true); 
ivLoad.setAnimation(LoadAnim); 
LoadAnim.start(); 

} 

當我試圖使旋轉只有當值發生了變化,然後它工作只有當他們正在改變,但如果他們不箭頭跳回到零位。是不是讓setfillafter告訴圖像它應該保持新的位置?

代碼:

public void setTacho() 

{ 
//rotate Tachonadel 
Rpmcurrentdegree=Rpmcurrentdegree+Rpmdegree; 
Rpmdegree=((rpms-lastrpm)*RPMtoDegree); 


if(Rpmdegree!=0) 
{ 
RpmAnim=new RotateAnimation((float)Rpmcurrentdegree, (float)Rpmdegree,  ivNadel.getWidth()/2, ivNadel.getHeight()/2); 
RpmAnim.setFillEnabled(true); 
RpmAnim.setFillAfter(true); 
ivNadel.setAnimation(RpmAnim); 
RpmAnim.start(); 
} 

//rotate Boostbalken 
currentBoostDegree=currentBoostDegree+BoostDegree; 
BoostDegree=(boost-lastBoost)*BOOSTtoDegree; 
//rotate Loadbalken 
currentLoadDegree=currentLoadDegree+LoadDegree; 
LoadDegree=(load-lastLoad)*LOADtoDegree; 

if(BoostDegree!=0) 
{ 
BoostAnim=new RotateAnimation((float)-currentBoostDegree, (float)-BoostDegree,  ivBoost.getWidth()/2, ivBoost.getHeight()/2); 
BoostAnim.setFillEnabled(true); 
BoostAnim.setFillAfter(true); 
ivBoost.setAnimation(BoostAnim); 
BoostAnim.start(); 
} 




if(LoadDegree!=0) 
{ 
LoadAnim=new RotateAnimation((float)currentLoadDegree, (float)LoadDegree,  ivLoad.getWidth()/2, ivLoad.getHeight()/2); 
LoadAnim.setFillEnabled(true); 
LoadAnim.setFillAfter(true); 
ivLoad.setAnimation(LoadAnim); 
LoadAnim.start(); 
} 
} 

我不明白=(

THX 4幫助

編輯:調用回調

藍牙線程的

部分

while (run) { 
       try { 

        bytes = mmInStream.read(buffer); 

        if (connection.btCallback != null) 
        { 
          connection.btCallback.getData(buffer,bytes); 
        } 

       } catch (IOException e) { 

        break; 
       } 

藍牙線程的回調梅索德:

public void getData(byte[] bytes, int len) 
     { 
      setTacho(); 
     } 
+0

您是在單獨的線程或UI線程上運行循環代碼嗎? – waqaslam 2012-03-23 10:45:13

+0

應該是一個單獨的線程。 – 5w4rley 2012-03-23 10:57:29

+0

但我在兩者都嘗試過,並沒有改變任何東西 – 5w4rley 2012-03-23 11:00:21

回答

0

運行你的旋轉代碼在一個單獨的線程,所以你可以將動畫設置爲您ImageViews如下:

//for example 
ivNadel.post(new Runnable() { 
    @Override 
    public void run() { 
     ivNadel.setAnimation(RpmAnim); 
    } 
}); 

看它是否幫助你

+0

該死的沒有幫助。仍然緩慢=( – 5w4rley 2012-03-27 06:47:48

+0

你可以把它放到一個簡單的項目,上傳在線和給我的鏈接... – waqaslam 2012-03-27 06:50:17

+0

嗯,我終於做到了。我改變了rotateanimations一個matrix.postrotate。現在它工作更好 – 5w4rley 2012-03-27 07:34:00