今天在我的第一个应用《那些花儿》(养花助手),主页上加了两只翩翩起舞的蝴蝶。
效果图如下:
步骤也是很简单。
1.在anim文件夹内定义动画XML文件
android:oneshot="false">
2.在main.xml页面定义一个ImageView用来显示动画。
3.在Activity中调用Aniamtion:
image.setBackgroundResource(R.anim.butterfly);
image2.setBackgroundResource(R.anim.leftbutterfly);
AnimationDrawable animation = (AnimationDrawable) image.getBackground();
animation.start();
AnimationDrawable animation2 = (AnimationDrawable) image2.getBackground();
animation2.start();
这里获取到了animation,要开始动画只需animation.start();即可。
但问题是,我的2.3机子不能播放。因为不能在Activity的onCreate()方法里调用该方法,此时AnimationDrawable类尚未完全与window接触,可以安排一个TouchEvent触发启动animation,如果希望一开始就播放动画,就要加入一个onWindowFocusChanged()方法来启动,
public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); animation.start(); }