2011-12-05 48 views
1

我使用5-6個線程一次顯示一個動畫。我正在使用翻譯動畫以及框架動畫。這是一個好主意嗎?什麼是最好的方式來做到這一點?我的代碼如下。在動畫中使用5-6個線程是一個好主意嗎?

Handler handler1 = new Handler();   
handler1.postDelayed(new Runnable() { 
    public void run() { 
    Animation animation = new TranslateAnimation(0 ,0 ,0 ,100); 
    animation.setDuration(800); 
    ImageView rimg2 = (ImageView) findViewById(R.id.rainfall); 
    animation.setRepeatCount(-1); 
    rimg2.startAnimation(animation); 

    Animation animation1 = new TranslateAnimation(-290, 290,0, -150); 
    animation1.setDuration(35000); 
    ImageView rimg = (ImageView) findViewById(R.id.cloud1); 
    rimg.setVisibility(View.VISIBLE); 
    animation1.setRepeatCount(-1); 
    rimg.startAnimation(animation1); 
    } 
},0); 

Handler handler2 = new Handler(); 
handler2.postDelayed(new Runnable() { 
    public void run() { 
    Animation animation = new TranslateAnimation(0 ,0 ,0 ,100); 
    animation.setDuration(800); 
    ImageView rimg3 = (ImageView) findViewById(R.id.drops); 
    animation.setRepeatCount(-1); 
    rimg3.startAnimation(animation); 
    rimg3.setVisibility(View.VISIBLE); 
    } 
},1000); 

Handler handler3 = new Handler(); 
handler3.postDelayed(new Runnable() { 
    public void run() { 
    Animation animation = new TranslateAnimation(0 ,0 ,-250 ,10); 
    animation.setDuration(800); 
    ImageView rimg2 = (ImageView) findViewById(R.id.rainfall1); 
    animation.setRepeatCount(-1); 
    rimg2.startAnimation(animation); 
    rimg2.setVisibility(View.VISIBLE); 
    } 
},5000); 

Handler handler4 = new Handler(); 
handler4.postDelayed(new Runnable() { 
    public void run() { 
    Animation animation = new TranslateAnimation(0 ,0 ,-150 ,10); 
    animation.setDuration(800); 
    ImageView rimg3 = (ImageView) findViewById(R.id.drops1); 
    animation.setRepeatCount(-1); 
    rimg3.startAnimation(animation); 
    rimg3.setVisibility(View.VISIBLE); 
    } 
},10000); 

Handler handler5 = new Handler(); 
handler5.postDelayed(new Runnable() { 
    public void run() { 
    Animation animation1 = new TranslateAnimation(-290, 290,0, -150); 
    animation1.setDuration(35000); 
    ImageView rimg = (ImageView) findViewById(R.id.cloud2); 
    rimg.setVisibility(View.VISIBLE); 
    animation1.setRepeatCount(-1); 
    rimg.startAnimation(animation1); 
    } 
},15000); 

回答

1

除非有您的代碼示例中未顯示的原因,你並不需要張貼的Runnable運行這些動畫。您可以從主UI線程啓動動畫。欲瞭解更多詳情,看看這裏的文檔:

http://developer.android.com/guide/topics/graphics/view-animation.html

這裏

http://developer.android.com/reference/android/view/animation/Animation.html

,如果你想錯開動畫的開始時間,您還可以設置開始偏移。

如果你想要做的事當動畫結束時,你可以用你的建議Animation.setAnimationListener

+0

抱歉和感謝.. – yshak

相關問題