在drawable文件夹中建立文件frame.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"> //true表示只播放一次,false表示循环播放
<item android:drawable="@drawable/g_1" android:duration="200" />
<item android:drawable="@drawable/g_2" android:duration="200" />
<item android:drawable="@drawable/g_3" android:duration="200" />
<item android:drawable="@drawable/g_4" android:duration="200" />
</animation-list>
@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = (TextView) this.findViewById(R.id.textView);
textView.setBackgroundResource(R.drawable.frame);//绑定Frame动画图形
final AnimationDrawable drawable = (AnimationDrawable) textView.getBackground();
//添加消息队列的回调,因为只有在frame动画加载完毕之后调用start方法才能使动画生效,或者不要将start方法放在oncreate中实现
getMainLooper().myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
public boolean queueIdle() {
drawable.start();//启动动画
return false;
}
});