2012-11-24 18 views
0

我在我的應用程序中的對象Drawable這是在屏幕上移動:如何在Android中爲Drawable創建動畫?

public class MyDrawable extends Drawable { 

    @Override 
    public void draw(Canvas canvas) { 
     // ... 
    } 
    // ... 
} 

它工作得很好,到目前爲止,但現在我有這個動畫Drawable。我查閱了文檔,看起來對我來說最好的選擇是使用AnimationDrawable

我做了這樣的事情:

public void addAnimation() { 
    animation = new AnimationDrawable(); 
    animation.addFrame(resources.getDrawable(R.drawable.anim_0), 100); 
    animation.addFrame(resources.getDrawable(R.drawable.anim_1), 100); 
    animation.addFrame(resources.getDrawable(R.drawable.anim_2), 100); 
    animation.addFrame(resources.getDrawable(R.drawable.anim_3), 100); 
    animation.setOneShot(false); 
    refreshAnimationBounds(); 
    animation.start(); 
} 

@Override 
public void draw(Canvas canvas) { 
    refreshAnimationBounds(); 
    animation.draw(canvas); 
} 

什麼情況是,如預期,但我只看到第一幀我原來Drawable移動。可能是什麼問題呢?

refreshAnimationBounds();只是刷新了我的Drawable對象(重新定位它)的邊界。

編輯:

常見:

public void onWindowFocusChanged(boolean hasFocus) { 
    if (hasFocus) { 
     animation.start(); 
    } else { 
     animation.stop(); 
    } 
} 

的解決方案是行不通的。

回答

0

一般來說動畫軟件包相當有問題。我記得我有同樣的問題動畫沒有開始,我帶來的解決方案是開始它不是在onCreate(),因爲我以前做,但onWindowFocusChanged()一旦所有視圖的東西設置。我沒有自己擴展AnimationDrawable,但它可能與您的情況相同。

+0

沒有這樣的方法。我不是使用'Animation',而是'AnimationDrawable',這是另一回事。 –

+0

正確。看到編輯 –

+0

正如我已經告訴你**沒有'setRepeatCount' **方法來調用。使用'onWindowFocusChanged'也沒有幫助。我沒有擴展'AnimationDrawable',而是將一個放在我的'Drawable'中。 –