Android动画Animation之Frame实现类似电影的动画效果


Frame 动画,即顺序播放事先做好的图像,跟电影类似。

接下来的案例是点击按钮实现播放动画,点击停止实现停止动画播放!

1、效果图:

      

2、main.xml文件很简单:

[html]

  1. <Button   
  2.         Android:layout_width="wrap_content"  
  3.         android:layout_height="wrap_content"  
  4.         android:id="@+id/button"  
  5.         android:text="开始"/>  
  6.     <Button   
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:id="@+id/stop"  
  10.         android:text="停止"/>  
  11.     <ImageView   
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_gravity="center_vertical"  
  15.         android:id="@+id/image"/>  
3、然后在drawable文件夹下定义一个frame.xml,为的是把所有的图片都定义在该xml文件中,它是一个animation列表,存放所有使用到的图片!!
[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3. <item android:drawable="@drawable/girl_1" android:duration="100" />  
  4.     <item android:drawable="@drawable/girl_2" android:duration="100" />  
  5.     <item android:drawable="@drawable/girl_3" android:duration="100" />  
  6.     <item android:drawable="@drawable/girl_4" android:duration="100" />  
  7.     <item android:drawable="@drawable/girl_5" android:duration="200" />  
  8.     <item android:drawable="@drawable/girl_6" android:duration="500" />  
  9.     <item android:drawable="@drawable/girl_7" android:duration="500" />  
  10.     <item android:drawable="@drawable/girl_8" android:duration="200" />  
  11.     <item android:drawable="@drawable/girl_9" android:duration="100" />  
  12.     <item android:drawable="@drawable/girl_10" android:duration="100" />  
  13.     <item android:drawable="@drawable/girl_11" android:duration="100" />  
  14. </animation-list>  
每个item里面有个duration属性,该属性为了使该图片持续多长时间!!

4、最后是java代码:

[java]

  1. package cn.csdn.anim;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.AnimationDrawable;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.ImageView;  
  10.   
  11. public class FrameActivity extends Activity implements OnClickListener {  
  12.     private Button button, stop;  
  13.     private ImageView image;  
  14.     private AnimationDrawable attackAnimation;//定义动画的对象   
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         init();  
  21.     }  
  22.   
  23.     private void init() {  
  24.         image = (ImageView) findViewById(R.id.image);//显示动画的imageview   
  25.         button = (Button) findViewById(R.id.button);//开始动画   
  26.         stop = (Button) findViewById(R.id.stop);//停止动画   
  27.         button.setOnClickListener(this);  
  28.         stop.setOnClickListener(this);  
  29.         image.setBackgroundResource(R.drawable.frame);//设置显示动画的image的背景资源参数是int,就是你自己写的frame.xml,里面是所有相关的图片   
  30.         attackAnimation = (AnimationDrawable) image.getBackground();  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onClick(View v) {  
  35.         switch (v.getId()) {  
  36.         case R.id.button:  
  37.             attackAnimation.start();//开始动画   
  38.             break;  
  39.         case R.id.stop:  
  40.             attackAnimation.stop();//停止动画   
  41.             break;  
  42.         }  
  43.     }  
  44. }  


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 >元素,一系列的嵌套的<项目>标签。 每一项定义了一个帧动画

相关内容