逐帧动画以很短的时间间隔连续显示一系列图像的过程。从编程角度讲:程序以固定时间逐个绘制一系列帧(图形)。
在Android中,使用AnimationDrawable完成逐帧动画的编码。
首先介绍AnimationDrawable:一个用来创建逐帧动画的类,定义了一系列的Drawable对象,这些对象可用作view对象的背景。
其次以一个例子介绍逐帧动画的最简单的实现方式:
1、新建project,activity名为FrameAnimationActivity,相应布局文件为:activity_frame_animation.xml,代码为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" tools:context=".FrameAnimationActivity" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/startButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/startAnimation"/> <ImageView android:id="@+id/animationImage" android:contentDescription="@string/app_name" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
其中,ImageView用于播放动画,Button用于控制动画开始和结束。
2、准备若干动画中的图片
3、在res/drawable/ 中创建一个xml文件,名为frame_animation.xml,文件用于定义动画:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/fish0" android:duration="100"/> <item android:drawable="@drawable/fish1" android:duration="100"/> <item android:drawable="@drawable/fish2" android:duration="100"/> <item android:drawable="@drawable/fish3" android:duration="100"/> <item android:drawable="@drawable/fish4" android:duration="100"/> <item android:drawable="@drawable/fish5" android:duration="100"/> <item android:drawable="@drawable/fish6" android:duration="100"/> <item android:drawable="@drawable/fish7" android:duration="100"/> <item android:drawable="@drawable/fish8" android:duration="100"/> <item android:drawable="@drawable/fish9" android:duration="100"/>
</animation-list>
其中 animation-list标记将对应于一个AnimationDrawable对象,每一个item指向一副图片,对应于动画中的一帧,属性duration为播放时间
4、在FrameAnimationActivity编写代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_frame_animation); inits(); } private void inits() { strButton = (Button) findViewById(R.id.startButton); animationView = (ImageView) findViewById(R.id.animationImage); animationView.setBackgroundResource(R.drawable.fish0); strButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub animationView.setBackgroundResource(R.drawable.frame_animation); AnimationDrawable frameAnimation = (AnimationDrawable) animationView .getBackground(); if (frameAnimation.isRunning()) { frameAnimation.stop(); strButton.setText(R.string.startAnimation); } else { frameAnimation.start(); strButton.setText(R.string.endAnimation); } } }); }
其中,关键代码:animationView.setBackgroundResource(R.drawable.frame_animation);
将xml实例化为Animation对象并设置为ImageView的背景 AnimationDrawable frameAnimation = (AnimationDrawable) animationView
获取通过xml文件定义并实例化后的Animation对象
frameAnimation.isRunning()判断动画是否正在运行
frameAnimation.start();开始动画
frameAnimation.stop();结束动画
最后,运行程序,观察逐帧效果。