默认情况下,不能在OnCreate()中执行animation.start();是无效的,因为在OnCreate()中AnimationDrawable还没有完全的与ImageView绑定,在OnCreate()中启动动画,就只能看到第一张图片。
要实现OnCreate()加载完立即播放动画怎么办呢?解决办法是,用Handler。
package info.rebill.animation;
import Android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
public class Main extends Activity {
private ImageView btnPlay;
private AnimationDrawable playAnim;
private Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnPlay = (ImageView) findViewById(R.id.btn_play);
handler.postDelayed(new Runnable() {
public void run() {
playAnim = (AnimationDrawable) btnPlay.getBackground();
playAnim.start();
}
}, 50);
}
}