新建一个工程,在res文件夹下新建anim文件夹,在anin文件夹下新建xml文件,将图片放到drawable文件夹中
activity中的代码如下:
package com.mrzhu.myframe;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.view.MotionEvent;
import android.widget.ImageView;
public class MyFrameActivity extends Activity {
//声明一个imageView和draw
private ImageView imgaeView;
private AnimationDrawable draw;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得ImageView控件
imgaeView = (ImageView) findViewById(R.id.imageView2);
//将要实现的动画绑定到draw上
draw = (AnimationDrawable) imgaeView.getBackground();
//获得主线程,将动画加到主线程当中
getMainLooper();
Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
public boolean queueIdle() {
//开始动画
draw.start();
return false;
}
});
}
anim文件夹下的xml代码如下:
将要显示的图片从drawable中取出来,设置每张图片的显示时间为100毫秒
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" >
<item android:drawable="@drawable/a" android:duration="100" />
<item android:drawable="@drawable/b" android:duration="100" />
<item android:drawable="@drawable/c" android:duration="100" />
<item android:drawable="@drawable/d" android:duration="100" />
<item android:drawable="@drawable/e" android:duration="100" />
<item android:drawable="@drawable/f" android:duration="100" />
<item android:drawable="@drawable/g" android:duration="100" />
<item android:drawable="@drawable/h" android:duration="100" />
<item android:drawable="@drawable/i" android:duration="100" />
</animation-list>
main.xml文件的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/frame" />
</LinearLayout>
资源下载:http://download.youkuaiyun.com/detail/zlqqhs/4681550