2011-12-06 45 views
2

我想向AnimationDrawable添加幀和持續時間的問題,但我無法得到我想要的結果。我可以添加幀,但隨後的調用不會清除舊幀。有沒有辦法重置AnimationDrawable?AnimationDrawable無法清除幀

這裏是從我的代碼的摘錄:

public void animate() { 

    if (a != null && a.isRunning()) { 
     a.stop(); 
    } 

    //iv.setImageDrawable(null); 
    //iv.setBackgroundDrawable(null); 
    iv.setBackgroundResource(R.drawable.blank_animation); 
    a = (AnimationDrawable) iv.getBackground(); 

    a.addFrame(res.getDrawable(R.drawable.blank), 1); 
    for (int i=0;i<Frames.size();i++) { 

     a.addFrame(res.getDrawable(Frames.get(i)), AnimationSpeed); 

    } 
    a.addFrame(res.getDrawable(R.drawable.blank), 1); 

    a.setOneShot(true); 
    a.start(); 

} 

我曾嘗試沒有成功下列去清除ImageView的和/或AnimationDrawable。

iv.setImageDrawable(null); 
iv.setBackgroundDrawable(null); 
a = new AnimationDrawable(); 
iv.clearAnimation(); 

我也嘗試了null ImageView並重新創建它,並沒有重置幀。

任何幫助將不勝感激。

回答

3

嘗試這樣

a = new AnimationDrawable(); 
a.addFrame(res.getDrawable(R.drawable.blank), 1); 
for (int i=0;i<Frames.size();i++) { 
    a.addFrame(res.getDrawable(Frames.get(i)), AnimationSpeed); 
} 
a.addFrame(res.getDrawable(R.drawable.blank), 1); 
iv.setBackgroundDrawable(a); 

a.setOneShot(true); 
a.start(); 
+0

感謝@ jobesu14奏效!唯一的問題是a.setBackgroundDrawable(anim);應該是iv.setBackgroundDrawable(a);. ImageView必須設置爲AnimationDrawable。 – SunMan

+0

當然你是對的。我會糾正我的答案。 – jobesu14

+0

這對我有用。 –