程序截图:
原理:
其实所谓的帧动画,说白了,就是每隔一段时间显示一张图片.......
实现步骤如下:
1、/res/drawable/下放入各种图片(即你要用来制作动画的图片),然后新建一个frame.xml的文件用来决定图片是显示顺序
frame.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/a"
android:duration="200" />
<item
android:drawable="@drawable/b"
android:duration="200" />
<item
android:drawable="@drawable/c"
android:duration="200" />
<item
android:drawable="@drawable/d"
android:duration="200" />
<item
android:drawable="@drawable/e"
android:duration="200" />
<item
android:drawable="@drawable/f"
android:duration="200" />
</animation-list>
其中item的写法可以去android官网中查看API。。。。
2、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/frame"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:onClick="start"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结束"
android:onClick="stop"
/>
</LinearLayout>
3、MainActivity
package com.njupt.frame1;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
private AnimationDrawable ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView) findViewById(R.id.iv);
}
public void start(View v){
ad = (AnimationDrawable) iv.getBackground();
ad.start();
}
public void stop(View v){
ad.stop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}