android 动画属性(二)之 Frame动画

Frame动画

1.在android当中动画效果是分为:Frame动画和Tween动画的Frame动画则简单得多了,就是把一张张的图片连续播放产生动画效果;Tween动画主要包括图片的放大缩小、旋转、透明度变化、移动等等操作;
这里就简单介绍一下Frame动画。

Frame动画主要是通过AnimationDrawable类来实现的,其中有两个方法start()和stop(),是用来控制动画的启动和停止的。

Frame动画一般通过XML文件配置,在工程的res/anim目录下创建一个XML配置文件,该配置文件有一个<animation-list>根元素和若干个<item>子元素。

下面就通过在XML文件中配置动画来展示效果:

Res里面创建一个anim文件夹并创建show.xml:

show.xml的为:   其中有个android:oneshot指示是否只运行一次,设置为false则意味着循环播放。

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:oneshot="false" >
 4 
 5     <item
 6         android:drawable="@drawable/p1"
 7         android:duration="500"/>
 8     <item
 9         android:drawable="@drawable/p2"
10         android:duration="500"/>
11     <item
12         android:drawable="@drawable/p3"
13         android:duration="500"/>
14     <item
15         android:drawable="@drawable/p4"
16         android:duration="500"/>
17     <item
18         android:drawable="@drawable/p5"
19         android:duration="500"/>
20     <item
21         android:drawable="@drawable/p6"
22         android:duration="500"/>
23 
24 </animation-list>

 

activity的activity_main.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"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/iamge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@anim/show" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止" />
    </LinearLayout>

</LinearLayout>

MainActivity:

 1 public class MainActivity extends Activity {
 2     private ImageView image;
 3     private Button start;
 4     private Button stop;
 5     private AnimationDrawable animationDrawable;
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_main);
10         image = (ImageView) findViewById(R.id.iamge);
11         start = (Button) findViewById(R.id.start);
12         stop = (Button) findViewById(R.id.stop);
13 
14         // 获取AnimationDrawable来控制图片显示和开始,  show.xml文件在activity_main.xml中已经赋给ImageView了
15         animationDrawable = (AnimationDrawable) image.getBackground();
16         start.setOnClickListener(new Button.OnClickListener() {
17 
18             @Override
19             public void onClick(View v) {
20                 animationDrawable.start();
21             }
22         });
23 
24         stop.setOnClickListener(new Button.OnClickListener() {
25 
26             @Override
27             public void onClick(View v) {
28                 animationDrawable.stop();
29             }
30         });
31 
32     }
33 
34 }

运行后的效果是:

 

 

源码下载地址

 

 

 

 

posted @ 2014-06-05 19:35  perfect亮  阅读(1129)  评论(0编辑  收藏  举报