Frame 动画,即顺序播放事先做好的图像,跟电影类似。接下来的案例是点击按钮实现播放动画,点击停止实现停止动画播放!1、效果图:
2、main.xml文件很简单:
[html] - <Button
- Android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button"
- android:text="开始"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/stop"
- android:text="停止"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:id="@+id/image"/>
3、然后在drawable文件夹下定义一个frame.xml,为的是把所有的图片都定义在该xml文件中,它是一个animation列表,存放所有使用到的图片!!
[html] - <?xml version="1.0" encoding="utf-8"?>
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
- <item android:drawable="@drawable/girl_1" android:duration="100" />
- <item android:drawable="@drawable/girl_2" android:duration="100" />
- <item android:drawable="@drawable/girl_3" android:duration="100" />
- <item android:drawable="@drawable/girl_4" android:duration="100" />
- <item android:drawable="@drawable/girl_5" android:duration="200" />
- <item android:drawable="@drawable/girl_6" android:duration="500" />
- <item android:drawable="@drawable/girl_7" android:duration="500" />
- <item android:drawable="@drawable/girl_8" android:duration="200" />
- <item android:drawable="@drawable/girl_9" android:duration="100" />
- <item android:drawable="@drawable/girl_10" android:duration="100" />
- <item android:drawable="@drawable/girl_11" android:duration="100" />
- </animation-list>
每个item里面有个duration属性,该属性为了使该图片持续多长时间!!4、最后是java代码:
[java] - package cn.csdn.anim;
-
- 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 Button button, stop;
- private ImageView image;
- private AnimationDrawable attackAnimation;//定义动画的对象
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
-
- private void init() {
- image = (ImageView) findViewById(R.id.image);//显示动画的imageview
- button = (Button) findViewById(R.id.button);//开始动画
- stop = (Button) findViewById(R.id.stop);//停止动画
- button.setOnClickListener(this);
- stop.setOnClickListener(this);
- image.setBackgroundResource(R.drawable.frame);//设置显示动画的image的背景资源参数是int,就是你自己写的frame.xml,里面是所有相关的图片
- attackAnimation = (AnimationDrawable) image.getBackground();
- }
-
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button:
- attackAnimation.start();//开始动画
- break;
- case R.id.stop:
- attackAnimation.stop();//停止动画
- break;
- }
- }
- }
AnimationDrawable类是:
An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object"s background.The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call
start() to run the animation.An AnimationDrawable defined in XML consists of a single
<animation-list> element, and a series of nested
<item> tags. Each item defines a frame of the animation.一个对象用于创建frame-by-frame动画,其定义是由一系列的可画的对象,可以使用作为一个视图对象的背景情况。
最简单的方法来创建一个frame-by-frame动画定义动画在XML文件中,放置在杂志/可画/文件夹,并将它设置为背景,一个视图对象。然后,叫开始()运行的动画。
定义一个AnimationDrawable在XML组成一个单一的< animation-list >元素,一系列的嵌套的<项目>标签。 每一项定义了一个帧动画