/res/drawable/flag.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flaganim"
android:oneshot="false"
>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flaganim"
android:oneshot="false"
>
<!--android:duration="xxx"控制该图片显示时间长度-->
<item android:drawable="@drawable/f03" android:duration="100" />
<item android:drawable="@drawable/f04" android:duration="100" />
<item android:drawable="@drawable/f05" android:duration="100" />
<item android:drawable="@drawable/f06" android:duration="100" />
<item android:drawable="@drawable/f07" android:duration="100" />
<item android:drawable="@drawable/f08" android:duration="100" />
<item android:drawable="@drawable/f09" android:duration="100" />
<item android:drawable="@drawable/f10" android:duration="100" />
</animation-list>
/res/layout/splash.xml
<item android:drawable="@drawable/f03" android:duration="100" />
<item android:drawable="@drawable/f04" android:duration="100" />
<item android:drawable="@drawable/f05" android:duration="100" />
<item android:drawable="@drawable/f06" android:duration="100" />
<item android:drawable="@drawable/f07" android:duration="100" />
<item android:drawable="@drawable/f08" android:duration="100" />
<item android:drawable="@drawable/f09" android:duration="100" />
<item android:drawable="@drawable/f10" android:duration="100" />
</animation-list>
/res/layout/splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SplashImageView"
android:layout_gravity="center"
>
</ImageView>
<!--
Not animated
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SplashImageView"
android:src="http://blog.163.com/hero_213/blog/@drawable/lnxins"
android:layout_gravity="center"
>
-->
</LinearLayout>
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) 动态的添加一个图片进入该动画中