Android 帧(frame)动画
帧动画是根据视觉停留原理而实现的动画效果,本例从AnimationDrawable角度来实现一个简单动画效果,主页面如图:http://dl.iteye.com/upload/attachment/0075/9786/9b4d8e69-a725-39f1-bfc2-20ecd1ae63fe.png
示例用含有数字1、2、3、4的图片代表不同的帧,然后通过AnimationDrawable来控制帧动画。下面说明一下实现过程:
1、建立动画文件:
在res/anim目录中建立一个xml文件,名称任取,格式如下:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false" ><item android:drawable="@drawable/n1" android:duration="50" /><item android:drawable="@drawable/n2" android:duration="50" /><item android:drawable="@drawable/n3" android:duration="50" /><item android:drawable="@drawable/n4" android:duration="50" /></animation-list> 说明:
[*]android:oneshot:帧动画运行的次数,true表示运行一次,false表示循环播放;
[*]item:定义每帧的属性;
[*]android:drawable:指定该帧对应的图像资源;
[*]android:duration:该帧图像停留的时间。
2、装载并控制帧动画:
import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class FrameActivity extends Activity implements OnClickListener {private ImageView imageView;private AnimationDrawable animationDrawable;private Button startButton;private Button startOneShotButton;private Button stopButton;private Button pauseOrContinueButton;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);startButton = (Button) findViewById(R.id.button1);startButton.setOnClickListener(this);startOneShotButton = (Button) findViewById(R.id.button2);startOneShotButton.setOnClickListener(this);stopButton = (Button) findViewById(R.id.button3);stopButton.setOnClickListener(this);pauseOrContinueButton = (Button) findViewById(R.id.button4);pauseOrContinueButton.setOnClickListener(this);imageView = (ImageView) findViewById(R.id.imageView1);imageView.setBackgroundResource(R.anim.frame_animation);animationDrawable = (AnimationDrawable) imageView.getBackground();}private void start(boolean isOneShot) {if (animationDrawable != null) {if (animationDrawable.isRunning()) {animationDrawable.stop();}animationDrawable.setOneShot(isOneShot);animationDrawable.start();}}@Overridepublic void onClick(View view) {if (view == startButton) {start(false);return;}if (view == startOneShotButton) {start(true);return;}if (view == stopButton) {animationDrawable.stop();return;}if (view == pauseOrContinueButton) {if (animationDrawable.isRunning()) {animationDrawable.stop();} else {animationDrawable.start();}return;}}} 说明:
[*]start():开始播放动画;
[*]stop():停止播放动画;
[*]isRunning():判断动画是否正在播放;
[*]setOneShot():设置是否循环播放。
AnimationDrawable还有一些其它的控制动画的方法,具体参看Google开发者网站相关资料。:)
3、多说一句:
希望对你有所帮助,如需代码,请点击此处:=^_^=!
页:
[1]