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();
}
}
的解決方案是行不通的。
沒有這樣的方法。我不是使用'Animation',而是'AnimationDrawable',這是另一回事。 –
正確。看到編輯 –
正如我已經告訴你**沒有'setRepeatCount' **方法來調用。使用'onWindowFocusChanged'也沒有幫助。我沒有擴展'AnimationDrawable',而是將一個放在我的'Drawable'中。 –