首先,在res下创建anim文件夹,建立dance.xml,duration表示这个图片的播放时间,单位毫秒,的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"> <!-- true表示不循环 -->
<item android:drawable="@drawable/p1" android:duration="500" />
<item android:drawable="@drawable/p0" android:duration="500" />
<item android:drawable="@drawable/p2" android:duration="600" />
</animation-list>
然后,在layout下创建dances.xml,添加如下代码
<!-- Frame动画图片 -->
<ImageView
android:id="@+id/ImgDance"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
创建类Animation_,如下:
// 定义AnimationDrawable
private AnimationDrawable mAnimationDrawable = null;
private ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dances);
// 定义一个动画布局文件
img = (ImageView) findViewById(R.id.ImgDance);
// 装载动画布局文件(也可以写在xml中,即 背景图片)
img.setBackgroundResource(R.anim.dance);
// 构建动画
mAnimationDrawable = (AnimationDrawable) img.getBackground();
// 是否循环
// mAnimationDrawable.setOneShot(false);
// 设置该类显示的动画
img.setBackgroundDrawable(mAnimationDrawable);
}
/*
* 不能在Activity的onCreate()方法里调用start方法,因为此时AnimationDrawable类尚未完全与window接触,
* 可以安排一个TouchEvent触发启动animation
* ,如果希望一开始就播放动画,就要加入一个onWindowFocusChanged()方法来启动,
*/
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
mAnimationDrawable.start();
} else {
mAnimationDrawable.stop();
}
}
如果该动画页面还要跳转,要 等待动画结束再开始跳转
,可以添加线程,也可以用Timer事件,后面的时间就设置动画放多长的时间
Timer timer = new Timer();
TimerTask tast = new TimerTask() {
public void run() {
Intent intent = new Intent(Animation_.this, MainActivity.class);
startActivity(intent);
finish();
}
};
timer.schedule(tast, 1600);
更多请关注我的新浪微博博客地址:http://blog.sina.com.cn/u/2804695260