[置顶] Android 用Animation-list实现逐帧动画

http://www.open-open.com/lib/view/open1344504946405.html

第一步:先上图片素材,以下素材放到res/drawable目录下:

图片素材:

Android 用Animation-list实现逐帧动画

Android 用Animation-list实现逐帧动画

Android 用Animation-list实现逐帧动画

文件名称:

icon1.png

icon1.png

icon1.png

icon1.png

icon1.png

icon1.png

第二步:上动画Animation-list帧布局文件,有2个,一个是按顺序显示动画,一个是倒序显示动画,文件存放在res/drawable目录下

顺序显示动画文件:animation1.xml

01 <?xmlversion="1.0"encoding="utf-8"?>
02 <!--
03     根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
04     根标签下,通过item标签对动画中的每一个图片进行声明
05     android:duration 表示展示所用的该图片的时间长度
06  -->
07 <animation-list
08   xmlns:android="http://schemas.android.com/apk/res/android"
09   android:oneshot="true"
10   >
11     <itemandroid:drawable="@drawable/icon1"android:duration="150"></item>
12     <itemandroid:drawable="@drawable/icon2"android:duration="150"></item>
13     <itemandroid:drawable="@drawable/icon3"android:duration="150"></item>
14     <itemandroid:drawable="@drawable/icon4"android:duration="150"></item>
15     <itemandroid:drawable="@drawable/icon5"android:duration="150"></item>
16     <itemandroid:drawable="@drawable/icon6"android:duration="150"></item>
17 </animation-list>

 

倒序显示动画文件:animation2.xml

01 <?xmlversion="1.0"encoding="utf-8"?>
02 <!--
03     根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
04     根标签下,通过item标签对动画中的每一个图片进行声明
05     android:duration 表示展示所用的该图片的时间长度
06  -->
07 <animation-list
08   xmlns:android="http://schemas.android.com/apk/res/android"
09   android:oneshot="true"
10   >
11     <itemandroid:drawable="@drawable/icon6"android:duration="150"></item>
12     <itemandroid:drawable="@drawable/icon5"android:duration="150"></item>
13     <itemandroid:drawable="@drawable/icon4"android:duration="150"></item>
14     <itemandroid:drawable="@drawable/icon3"android:duration="150"></item>
15     <itemandroid:drawable="@drawable/icon2"android:duration="150"></item>
16     <itemandroid:drawable="@drawable/icon1"android:duration="150"></item>
17 </animation-list>


第三步:上布局文件,放在res/layout目录下,文件名main.xml

01 <?xmlversion="1.0"encoding="utf-8"?>
02 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
03    android:layout_width="fill_parent"
04    android:layout_height="fill_parent"
05    android:orientation="vertical">
06     
07     <ImageViewandroid:id="@+id/animationIV"
08             android:layout_width="wrap_content"
09             android:layout_height="wrap_content"
10             android:padding="5px"
11             android:src="@drawable/animation1"/>
12              
13     <Buttonandroid:id="@+id/buttonA"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:padding="5px"
17         android:text="顺序显示"/>
18      
19     <Buttonandroid:id="@+id/buttonB"
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:padding="5px"
23         android:text="停止"/>
24      
25     <Buttonandroid:id="@+id/buttonC"
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
28         android:padding="5px"
29         android:text="倒序显示"/>
30  
31 </LinearLayout>

第四步:上Activity文件,文件名:MainActivity.java

01 package org.shuxiang.test;
02  
03 import android.app.Activity;
04 import android.graphics.drawable.AnimationDrawable;
05  
06 import android.os.Bundle;
07 import android.view.View;
08 import android.view.View.OnClickListener;
09 import android.view.Window;
10 import android.widget.Button;
11 import android.widget.ImageView;
12  
13 public classActivity10 extends Activity
14 {
15     privateImageView animationIV;
16     privateButton buttonA, buttonB, buttonC;
17     privateAnimationDrawable animationDrawable;
18     @Override
19     publicvoid onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         requestWindowFeature(Window.FEATURE_NO_TITLE);
22         setContentView(R.layout.test10);
23         
24          
25         animationIV = (ImageView) findViewById(R.id.animationIV);
26         buttonA = (Button) findViewById(R.id.buttonA);
27         buttonB = (Button) findViewById(R.id.buttonB);
28         buttonC = (Button) findViewById(R.id.buttonC);
29          
30         buttonA.setOnClickListener(newOnClickListener()
31         {
32             @Override
33             publicvoid onClick(View v) {
34                 // TODO Auto-generated method stub
35                 animationIV.setImageResource(R.drawable.animation1);
36                 animationDrawable = (AnimationDrawable) animationIV.getDrawable();
37                 animationDrawable.start();
38             }
39              
40         });
41          
42         buttonB.setOnClickListener(newOnClickListener()
43         {
44             @Override
45             publicvoid onClick(View v) {
46                 // TODO Auto-generated method stub
47                 animationDrawable = (AnimationDrawable) animationIV.getDrawable();
48                 animationDrawable.stop();
49             }
50              
51         });
52          
53         buttonC.setOnClickListener(newOnClickListener()
54         {
55             @Override
56             publicvoid onClick(View v) {
57                 // TODO Auto-generated method stub
58                 animationIV.setImageResource(R.drawable.animation2);
59                 animationDrawable = (AnimationDrawable) animationIV.getDrawable();
60                 animationDrawable.start();
61             }          
62         });       
63     }

64 }

你可能感兴趣的:([置顶] Android 用Animation-list实现逐帧动画)