/res/drawable/flag.xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flaganim"
android:oneshot="false"
>
/res/layout/splash.xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TheSplashLayout"
android:layout_gravity="center"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SplashImageView"
android:layout_gravity="center"
>
MainActivity.java
final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);
splashImageView.setBackgroundResource(R.drawable.flag);
final AnimationDrawable frameAnimation = (AnimationDrawable)splashImageView.getBackground();
splashImageView.post(new Runnable(){
@Override
public void run() {
frameAnimation.start();//启动动画
// frameAnimation.stop(); //关闭动画
}
});
除此之外:在AnimationDrawable中,我们还可以看到如下的几个重要方法:
setOneShot(boolean flag) 和在配置文件中进行配置一样,可以设置动画是否播放一次,false为连续播放;
addFrame (Drawable frame, int duration) 动态的添加一个图片进入该动画中
原文:http://www.cnblogs.com/xubuhang/p/4223502.html