摘自:http://www.open-open.com/lib/view/open1344504946405.html
第一步:先上图片素材,以下素材放到res/drawable目录下:
图片素材:
|
|
|
|
|
|
|
文件名称:
|
icon1.png
|
icon1.png
|
icon1.png
|
icon1.png
|
icon1.png
|
icon1.png
|
第二步:上动画Animation-list帧布局文件,有2个,一个是按顺序显示动画,一个是倒序显示动画,文件存放在res/drawable目录下
顺序显示动画文件:animation1.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
03 |
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画 |
04 |
根标签下,通过item标签对动画中的每一个图片进行声明 |
05 |
android:duration 表示展示所用的该图片的时间长度 |
08 |
xmlns:android = "http://schemas.android.com/apk/res/android" |
09 |
android:oneshot = "true" |
11 |
< item android:drawable = "@drawable/icon1" android:duration = "150" ></ item > |
12 |
< item android:drawable = "@drawable/icon2" android:duration = "150" ></ item > |
13 |
< item android:drawable = "@drawable/icon3" android:duration = "150" ></ item > |
14 |
< item android:drawable = "@drawable/icon4" android:duration = "150" ></ item > |
15 |
< item android:drawable = "@drawable/icon5" android:duration = "150" ></ item > |
16 |
< item android:drawable = "@drawable/icon6" android:duration = "150" ></ item > |
倒序显示动画文件:animation2.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
03 |
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画 |
04 |
根标签下,通过item标签对动画中的每一个图片进行声明 |
05 |
android:duration 表示展示所用的该图片的时间长度 |
08 |
xmlns:android = "http://schemas.android.com/apk/res/android" |
09 |
android:oneshot = "true" |
11 |
< item android:drawable = "@drawable/icon6" android:duration = "150" ></ item > |
12 |
< item android:drawable = "@drawable/icon5" android:duration = "150" ></ item > |
13 |
< item android:drawable = "@drawable/icon4" android:duration = "150" ></ item > |
14 |
< item android:drawable = "@drawable/icon3" android:duration = "150" ></ item > |
15 |
< item android:drawable = "@drawable/icon2" android:duration = "150" ></ item > |
16 |
< item android:drawable = "@drawable/icon1" android:duration = "150" ></ item > |
第三步:上布局文件,放在res/layout目录下,文件名main.xml:
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
android:layout_width = "fill_parent" |
04 |
android:layout_height = "fill_parent" |
05 |
android:orientation = "vertical" > |
07 |
< ImageView android:id = "@+id/animationIV" |
08 |
android:layout_width = "wrap_content" |
09 |
android:layout_height = "wrap_content" |
11 |
android:src = "@drawable/animation1" /> |
13 |
< Button android:id = "@+id/buttonA" |
14 |
android:layout_width = "wrap_content" |
15 |
android:layout_height = "wrap_content" |
17 |
android:text = "顺序显示" /> |
19 |
< Button android:id = "@+id/buttonB" |
20 |
android:layout_width = "wrap_content" |
21 |
android:layout_height = "wrap_content" |
25 |
< Button android:id = "@+id/buttonC" |
26 |
android:layout_width = "wrap_content" |
27 |
android:layout_height = "wrap_content" |
29 |
android:text = "倒序显示" /> |
第四步:上Activity文件,文件名:MainActivity.java
01 |
package org.shuxiang.test; |
03 |
import android.app.Activity; |
04 |
import android.graphics.drawable.AnimationDrawable; |
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; |
13 |
public class Activity10 extends Activity |
15 |
private ImageView animationIV; |
16 |
private Button buttonA, buttonB, buttonC; |
17 |
private AnimationDrawable animationDrawable; |
19 |
public void onCreate(Bundle savedInstanceState) { |
20 |
super .onCreate(savedInstanceState); |
21 |
requestWindowFeature(Window.FEATURE_NO_TITLE); |
22 |
setContentView(R.layout.test10); |
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); |
30 |
buttonA.setOnClickListener( new OnClickListener() |
33 |
public void onClick(View v) { |
34 |
// TODO Auto-generated method stub |
35 |
animationIV.setImageResource(R.drawable.animation1); |
36 |
animationDrawable = (AnimationDrawable) animationIV.getDrawable(); |
37 |
animationDrawable.start(); |
42 |
buttonB.setOnClickListener( new OnClickListener() |
45 |
public void onClick(View v) { |
46 |
// TODO Auto-generated method stub |
47 |
animationDrawable = (AnimationDrawable) animationIV.getDrawable(); |
48 |
animationDrawable.stop(); |
53 |
buttonC.setOnClickListener( new OnClickListener() |
56 |
public void onClick(View v) { |
57 |
// TODO Auto-generated method stub |
58 |
animationIV.setImageResource(R.drawable.animation2); |
59 |
animationDrawable = (AnimationDrawable) animationIV.getDrawable(); |
60 |
animationDrawable.start(); |