frame-by-frame animation 是一个展示一连串图片的简单的动画
实现frame-by-frame animation 的步骤
A将一连串图片存放在drawable文件夹中
B define the list of frames to be animated. the name of the file is aframe_animation.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/circle1" android:duration="50" />
<item android:drawable="@drawable/circle2" android:duration="50" />
<item android:drawable="@drawable/circle3" android:duration="50" />
</animation-list>
上面这个xml的作用时将这三张图片一次放映,时间间隔是50mill second
create XML Layout File for the Frame Animation Example
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@+id/textViewId1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Debuge Scrath pad"
/>
<Button
android:id = "@+id/startButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="start animation"/>
<ImageView
android:id = "@+id/animationImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
the main Activity
package hust.ophoneclub.FrameAnimation;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class FrameAnimation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupButton();
}
/**
*
*/
private void setupButton() {
Button btn =(Button)findViewById(R.id.startButtonId);
btn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
startAnimate();
}
});
}
private void startAnimate() {
ImageView imageView = setBackgroundOfImageView();
animate(imageView);
}
/**
* @param imageView
*/
private void animate(ImageView imageView) {
AnimationDrawable frameAnimation =
(AnimationDrawable)imageView.getBackground();
if(frameAnimation.isRunning()){
frameAnimation.stop();
}else{
frameAnimation.stop();
frameAnimation.start();
}
}
private ImageView setBackgroundOfImageView() {
ImageView imageView =(ImageView)findViewById(R.id.animationImage);
imageView.setVisibility(ImageView.VISIBLE);
imageView.setBackgroundResource(R.drawable.frame_animation);
return imageView;
}
}
说明:frameAnimation是作为main.xml中的background
imageView.setBackgroundResource(R.drawable.frame_animation);
这句代码的作用是将定义frame Animation 的 xml rescourse 文件转化为一个AnimationDrwable 对象
加到imageView的背景上 所以我们要取得这个对象来进行相关的操作
AnimationDrawable frameAnimation =
(AnimationDrawable)imageView.getBackground();
这句代码就取得了这个对象;
最后的效果是这样的