逐幀動畫:連續播放每一張圖檔,類似卡通片,實作動畫效果
第一步:先上圖檔素材,以下素材放到res/drawable目錄下:
圖檔素材: | ||||||
檔案名稱: | icon1.png | icon2.png | icon3.png | icon4.png | icon5.png | icon6.png |
第二步:上動畫Animation-list幀布局檔案,有2個,一個是按順序顯示動畫,一個是倒序顯示動畫,檔案存放在res/drawable目錄下
順序顯示動畫檔案:animation1.xml
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- 根标簽為animation-list,其中oneshot代表着是否隻展示一遍,設定為false會不停的循環播放動畫
- 根标簽下,通過item标簽對動畫中的每一個圖檔進行聲明
- android:duration 表示展示所用的該圖檔的時間長度
- -->
- <animation-list
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="true"
- >
- <item android:drawable="@drawable/icon1" android:duration="150"></item>
- <item android:drawable="@drawable/icon2" android:duration="150"></item>
- <item android:drawable="@drawable/icon3" android:duration="150"></item>
- <item android:drawable="@drawable/icon4" android:duration="150"></item>
- <item android:drawable="@drawable/icon5" android:duration="150"></item>
- <item android:drawable="@drawable/icon6" android:duration="150"></item>
- </animation-list>
倒序顯示動畫檔案:animation2.xml
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- 根标簽為animation-list,其中oneshot代表着是否隻展示一遍,設定為false會不停的循環播放動畫
- 根标簽下,通過item标簽對動畫中的每一個圖檔進行聲明
- android:duration 表示展示所用的該圖檔的時間長度
- -->
- <animation-list
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="true"
- >
- <item android:drawable="@drawable/icon6" android:duration="150"></item>
- <item android:drawable="@drawable/icon5" android:duration="150"></item>
- <item android:drawable="@drawable/icon4" android:duration="150"></item>
- <item android:drawable="@drawable/icon3" android:duration="150"></item>
- <item android:drawable="@drawable/icon2" android:duration="150"></item>
- <item android:drawable="@drawable/icon1" android:duration="150"></item>
- </animation-list>
第三步:上布局檔案,放在res/layout目錄下,檔案名main.xml:
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <ImageView android:id="@+id/animationIV"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="5px"
- android:src="@drawable/animation1"/>
- <Button android:id="@+id/buttonA"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="5px"
- android:text="順序顯示" />
- <Button android:id="@+id/buttonB"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="5px"
- android:text="停止" />
- <Button android:id="@+id/buttonC"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="5px"
- android:text="倒序顯示" />
- </LinearLayout>
第四步:上Activity檔案,檔案名:MainActivity.Java
[java] view plain copy
- package org.shuxiang.test;
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.ImageView;
- public class Activity10 extends Activity
- {
- private ImageView animationIV;
- private Button buttonA, buttonB, buttonC;
- private AnimationDrawable animationDrawable;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.test10);
- animationIV = (ImageView) findViewById(R.id.animationIV);
- buttonA = (Button) findViewById(R.id.buttonA);
- buttonB = (Button) findViewById(R.id.buttonB);
- buttonC = (Button) findViewById(R.id.buttonC);
- buttonA.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- animationIV.setImageResource(R.drawable.animation1);
- animationDrawable = (AnimationDrawable) animationIV.getDrawable();
- animationDrawable.start();
- }
- });
- buttonB.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- animationDrawable = (AnimationDrawable) animationIV.getDrawable();
- animationDrawable.stop();
- }
- });
- buttonC.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- animationIV.setImageResource(R.drawable.animation2);
- animationDrawable = (AnimationDrawable) animationIV.getDrawable();
- animationDrawable.start();
- }
- });
- }
- }
轉載:http://blog.csdn.net/aminfo/article/details/7847761