2014-03-01 36 views
0

我正在Android中執行一個簡單的動畫應用程序。這是場景。我有一個按鈕和4個文字瀏覽。當用戶點擊按鈕時,第一個文本視圖應該開始動畫(閃爍效果),然後是第二個文本視圖等等。這是我到目前爲止所嘗試的:Android java線程睡眠示例

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

    btGo.setEnabled(false); 
    glowCircuit2(); 
    glowCircuit3(); 
    glowCircuit4(); 


} 

private void glowCircuit2() { 

    new Thread(new Runnable() 
    { 

     @Override 
     public void run() 
     { 
      while (!Thread.interrupted()) 
       try 
       { 
        Thread.sleep(3000); 
        runOnUiThread(new Runnable() // start actions in UI thread 
        { 
         @Override 
         public void run() 
         { 
          // this action have to be in UI thread 
          tvCircuit2.setCompoundDrawablesWithIntrinsicBounds(0, 
            R.drawable.activecircuit, 0, 0); 
          tvCircuit2.startAnimation(animBlink); 

         } 
        }); 
       } 
       catch (InterruptedException e) 
       { 
        // ooops 
       } 
     } 
    }).start(); // the while thread will start in BG thread 

} 

private void glowCircuit3() { 

    new Thread(new Runnable() 
    { 

     @Override 
     public void run() 
     { 
      while (!Thread.interrupted()) 
       try 
       { 
        Thread.sleep(3000); 
        runOnUiThread(new Runnable() // start actions in UI thread 
        { 
         @Override 
         public void run() 
         { 
          // this action have to be in UI thread 
          tvCircuit3.setCompoundDrawablesWithIntrinsicBounds(0, 
            R.drawable.activecircuit, 0, 0); 
          tvCircuit3.startAnimation(animBlink); 
         } 
        }); 
       } 
       catch (InterruptedException e) 
       { 
        // ooops 
       } 
     } 
    }).start(); // the while thread will start in BG thread 

} 

private void glowCircuit4() { 

    new Thread(new Runnable() 
    { 

     @Override 
     public void run() 
     { 
      while (!Thread.interrupted()) 
       try 
       { 
        Thread.sleep(3000); 
        runOnUiThread(new Runnable() // start actions in UI thread 
        { 
         @Override 
         public void run() 
         { 
          // this action have to be in UI thread 
          tvCircuit4.setCompoundDrawablesWithIntrinsicBounds(0, 
            R.drawable.activecircuit, 0, 0); 
          tvCircuit4.startAnimation(animBlink); 
         } 
        }); 
       } 
       catch (InterruptedException e) 
       { 
        // ooops 
       } 
     } 
    }).start(); // the while thread will start in BG thread 

} 

我得到了閃爍的效果,但我得到了同步效果。我想要的是等待每個文本視圖進行動畫製作,而不是同時進行動畫製作。你有什麼聰明的想法來做到這一點?非常感謝幫助。謝謝。

+0

提示:要達到該目的,必須在glowCircuit2()內調用'glowCircuit3()' –

+0

您還沒有爲等待線程逐個做動畫做任何編碼。有多種方法可以實現這一點,你應該閱讀如何在線程上加入()。其他先進的方法,通過使用'障礙'來做同樣的事情。 – sakura

+0

@sakura nope,我用xml完成​​了閃爍的動畫。所以我現在想做的是等待textview在下一個textview出現之前顯示。 – Dunkey

回答

2

您在此處使用線程,您永遠不會知道哪個線程首先獲得優先級,哪個線程會開始。我建議你使用的處理程序,建立不同的郵件ID發送空消息處理程序,Android的做這樣的事情:

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    btGo.setEnabled(false); 
    handler.sendEmptyMessage(1); 


} 

Handler handler = new Handler(){ 
    onHandle(Messsage msg) 
    { 
     switch (msg.what) 
     { 
      case 1 : glowCircuit2(); 
      handler.sendEmptyMessage(2); 
      break; 
      case 2: glowCircuit3(); 
      handler.sendEmptyMessage(3); 
      break; 
     } 

    } 
}; 


private void glowCircuit2(){ 

    tvCircuit2.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.activecircuit, 0, 0); 
    tvCircuit2.startAnimation(animBlink); 

} 
+0

謝謝。那麼我會在哪裏放? – Dunkey

+0

@Dkey看到我編輯的答案。 – Yuvi

+0

所以我會把它放在一個方法中?我有點困惑,對不起。 – Dunkey

0

更改代碼

glowCircuit2(); 
    glowCircuit3(); 
    glowCircuit4(); 

這個

animation.setAnimationListener(new Animation.AnimationListener() 
    { 
     @Override 
     public void onAnimationStart(Animation animation) { 
     } 
     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 
     @Override 
     public void onAnimationEnd(Animation animation) { 
      glowCircuit2(); 
     } 
    }); 

    animation1.setAnimationListener(new Animation.AnimationListener() 
    { 
     @Override 
     public void onAnimationStart(Animation animation) { 
     } 
     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 
     @Override 
     public void onAnimationEnd(Animation animation) { 
      glowCircuit3(); 
     } 
    }); 

    animation2.setAnimationListener(new Animation.AnimationListener() 
    { 
     @Override 
     public void onAnimationStart(Animation animation) { 
     } 
     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 
     @Override 
     public void onAnimationEnd(Animation animation) { 
      glowCircuit4(); 
     } 
    }); 

像這段代碼在第二個動畫結束時開始你的第三個動畫,在第三個開始第四個動畫結束。