文章出處

Drawable 最強大的功能是:顯示Animation。AndroidSDK介紹了2種Animation:

  • Tween Animation(漸變動畫):通過對場景里的對象不斷做圖像變換(平移、縮放、旋轉)產生動畫效果
  • Frame Animation(幀動畫)   :順序播放事先做好的圖像,類似放電影

在使用Animation前,我們先學習如何定義Animation,這對我們使用Animation會有很大的幫助。Animation是以XML格式定義的,定義好的XML文件存放在res/anim中。 由于Tween Animation與Frame Animation的定義、使用都有很大的差異,我們將分開介紹,本篇幅中介紹Tween Animation的定義與使用,后續篇幅再詳細介紹Frame Animation。按照XML文檔的結構【父節點,子節點,屬性】來介紹Tween Animation,其由4種類型:

  • Alpha:漸變透明度動畫效果
  • Scale:漸變尺寸伸縮動畫效果
  • Translate:畫面轉換位置移動動畫效果
  • Rotate:畫面轉換位置移動動畫效果

在介紹以上4種類型前,先介紹Tween Animation共同的節點屬性。

表一

屬性[類型] 功能  
Duration[long] 屬性為動畫持續時間 時間以毫秒為單位
fillAfter [boolean] 當設置為true ,該動畫轉化在動畫結束后被應用
fillBefore[boolean] 當設置為true ,該動畫轉化在動畫開始前被應用

interpolator

指定一個動畫的插入器 有一些常見的插入器
accelerate_decelerate_interpolator
加速-減速 動畫插入器
accelerate_interpolator
加速-動畫插入器
decelerate_interpolator
減速- 動畫插入器
其他的屬于特定的動畫效果
repeatCount[int] 動畫的重復次數  
repeatMode[String] 定義重復的行為

1:"restart" 2:"reverse"   eg: android:repeatMode="reverse"

startOffset[long] 動畫之間的時間間隔,從上次動畫停多少時間開始執行下個動畫
zAdjustment[int] 定義動畫的Z Order的改變 0:保持Z Order不變
1:保持在最上層
-1:保持在最下層

看了以上節點,大家是不是都想開始定義動畫了。下面我們就開始結合具體的例子,介紹4種類型各自特有的節點元素。

表二

XML節點 功能說明
alpha 漸變透明度動畫效果
<alpha
android:fromAlpha=”0.1″
android:toAlpha=”1.0″
android:duration=”3000″ />
fromAlpha

屬性為動畫起始時透明度

0.0表示完全透明
1.0表示完全不透明
以上值取0.0-1.0之間的float數據類型的數字
toAlpha 屬性為動畫結束時透明度

表三

scale 漸變尺寸伸縮動畫效果
<scale
android:interpolator= “@android:anim/accelerate_decelerate_interpolator”
android:fromXScale=”0.0″
android:toXScale=”1.4″
android:fromYScale=”0.0″
android:toYScale=”1.4″
android:pivotX=”50%”
android:pivotY=”50%”
android:fillAfter=”false”
android:startOffset=“700”
android:duration=”700″
android:repeatCount=”10″ />
fromXScale[float] fromYScale[float] 為動畫起始時,X、Y坐標上的伸縮尺寸 0.0表示收縮到沒有
1.0表示正常無伸縮
值小于1.0表示收縮
值大于1.0表示放大
toXScale [float]
toYScale[float]
為動畫結束時,X、Y坐標上的伸縮尺寸
pivotX[float]
pivotY[float]
為動畫相對于物件的X、Y坐標的開始位置 屬性值說明:從0%-100%中取值,50%為物件的X或Y方向坐標上的中點位置
       

表四

translate 畫面轉換位置移動動畫效果
<translate
android:fromXDelta=”30″
android:toXDelta=”-80″
android:fromYDelta=”30″
android:toYDelta=”300″
android:duration=”2000″ />
fromXDelta
toXDelta
為動畫、結束起始時 X坐標上的位置  
fromYDelta
toYDelta
為動畫、結束起始時 Y坐標上的位置  
       

表五

rotate 畫面轉移旋轉動畫效果
<rotate
android:interpolator=”@android:anim/accelerate_decelerate_interpolator”
android:fromDegrees=”0″
android:toDegrees=”+350″
android:pivotX=”50%”
android:pivotY=”50%”
android:duration=”3000″ />
fromDegrees 為動畫起始時物件的角度 說明
當角度為負數——表示逆時針旋轉
當角度為正數——表示順時針旋轉
(負數from——to正數:順時針旋轉)
(負數from——to負數:逆時針旋轉)
(正數from——to正數:順時針旋轉)
(正數from——to負數:逆時針旋轉)
toDegrees 屬性為動畫結束時物件旋轉的角度 可以大于360度
pivotX
pivotY
為動畫相對于物件的X、Y坐標的開始位 說明:以上兩個屬性值 從0%-100%中取值
50%為物件的X或Y方向坐標上的中點位置
       

 

 

按照上面的講述學習完了Tween Animation的定義,對Tween Animation有了詳細的了解,再去了解下Android SDK的animation package(android.view.animation),其提供了操作Tween Animation所有的類。

Android SDK提供了基類:Animation,包含大量的set/getXXXX()函數來設置、讀取Animation的屬性,也就是前面表一中顯示的各種屬 性。Tween Animation由4種類型:alpha、scale、translate、roate,在Android SDK中提供了相應的類,Animation類派生出了AlphaAnimation、ScaleAnimation、 TranslateAnimation、RotateAnimation分別實現了平移、旋轉、改變 Alpha 值等動畫,每個子類都在父類的基礎上增加了各自獨有的屬性。再去看下這幾個類的構造函數,是不是與我們在表二、表三、表四、表五種定義的屬性完全一樣。

 在了解了Tween Animation的定義,對android.view.animation有了一些基本的認識后,開始介紹Tween Animation如何使用。Android SDK提供了2種方法:1、直接從XML資源中讀取Animation;2、使用Animation子類的構造函數來初始化Animation對象。第二種方法在看了Android SDK中各個類的說明就知道如何使用了,下面簡要說明從XML資源中讀取Animation,按照應用程序開發的過程,介紹整個使用的過程,如下:

  1. 創建Android工程;
  2. 導入一張圖片資源;
  3. 在res/layout/main.xml中添加一個 ImageView Widget;
  4. 在res下創建新的文件夾且命名為:anim,并在此文件夾下面定義 Animation XML 文件;
  5. 修改OnCreate()中的代碼,顯示動畫資源;

關鍵代碼,解析如下:


//main.xml中的ImageView


ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);


//加載動畫


Animation hyperspaceJumpAnimation =


AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);


//使用ImageView顯示動畫


spaceshipImage.startAnimation(hyperspaceJumpAnimation);


這里簡要解析如下:

  • AnimationUtils提供了加載動畫的函數,除了函數loadAnimation(),其他的到Android SDK中去詳細了解吧;
  • 所謂的動畫,也就是對 view 的內容做一次圖形變換;

 Android 中的 Animation 應用(二)

對Tween Animation的本質做個總結:Tween Animation通過對 View 的內容完成一系列的圖形變換 (包括平移、縮放、旋轉、改變透明度)來實現動畫效果。具體來講,預先定義一組指令,這些指令指定了圖形變換的類型、觸發時間、持續時間。這些指令可以是 以 XML 文件方式定義,也可以是以源代碼方式定義。程序沿著時間線執行這些指令就可以實現動畫效果。

在這里,我們需要對2個問題進行深入的解析:

  • 動畫的運行時如何控制的?
  • 動畫的運行模式。

如何控制動畫的運行?

這個問題,我們也就也就是上一篇幅中提到的Tween Animation,估計大家對什么是Interpolator、到底有什么作用,還是一頭霧水,在這里做個詳細的說明。按照Android SDK中對interpolator的說明:interpolator定義一個動畫的變化率(the rate of change)。這使得基本的動畫效果(alpha, scale, translate, rotate)得以加速,減速,重復等。

用通俗的一點的話理解就是:動畫的進度使用 Interpolator 控制。Interpolator 定義了動畫的變化速度,可以實現勻速、正加速、負加速、無規則變加速等。Interpolator 是基類,封裝了所有 Interpolator 的共同方法,它只有一個方法,即 getInterpolation (float input),該方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了幾個 Interpolator 子類,實現了不同的速度曲線,如下:

 

AccelerateDecelerateInterpolator 在動畫開始與介紹的地方速率改變比較慢,在中間的時候加速
AccelerateInterpolator 在動畫開始的地方速率改變比較慢,然后開始加速
CycleInterpolator 動畫循環播放特定的次數,速率改變沿著正弦曲線
DecelerateInterpolator 在動畫開始的地方速率改變比較慢,然后開始減速
LinearInterpolator 在動畫的以均勻的速率改變

對于 LinearInterpolator ,變化率是個常數,即 f (x) = x.

public float getInterpolation(float input) {

 return input;
}
Interpolator其他的幾個子類,也都是按照特定的算法,實現了對變化率。還可以定義自己的 Interpolator 子類,實現拋物線、自由落體等物理效果。

動畫的運行模式

動畫的運行模式有兩種:

  • 獨占模式,即程序主線程進入一個循環,根據動畫指令不斷刷新屏幕,直到動畫結束;
  • 中斷模式,即有單獨一個線程對時間計數,每隔一定的時間向主線程發通知,主線程接到通知后更新屏幕;

額外補充說明:Transformation 類

Transformation 記錄了仿射矩陣 Matrix,動畫每觸發一次,會對原來的矩陣做一次運算, View 的 Bitmap 與這個矩陣相乘就可實現相應的操作(旋轉、平移、縮放等)。Transformation 類封裝了矩陣和 alpha 值,它有兩個重要的成員,一是 mMatrix,二是 mAlpha。Transformation 類圖如下所示:


總結說明

圖形變換通過仿射矩陣實現。圖形變換是圖形學中的基本知識,簡單來講,每種變換都是一次矩陣運算。在 Android 中,Canvas 類中包含當前矩陣,當調用 Canvas.drawBitmap (bmp, x, y, Paint) 繪制時,Android 會先把 bmp 做一次矩陣運算,然后將運算的結果顯示在 Canvas 上。這樣,編程人員只需不斷修改 Canvas 的矩陣并刷新屏幕,View 里的對象就會不停的做圖形變換,因此就形成了動畫。

Android 中的 Animation 應用(三)

前面我們詳細介紹了Tween  Aniamation,這節我將介紹另外一種動畫Frame Animation。在前面已經說過,Frame Animation是順序播放事先做好的圖像,與電影類似。不同于animation package, Android SDK提供了另外一個類AnimationDrawable來定義、使用Frame Animation。

Frame Animation可以在XML Resource定義(還是存放到res/anim文件夾下),也可以使用AnimationDrawable中的API定義。 由于Tween Animation與Frame Animation有著很大的不同,因此XML定義的格式也完全不一樣,其格式是:首先是animation-list根節點,animation- list根節點中包含多個item子節點,每個item節點定義一幀動畫:當前幀的drawable資源和當前幀持續的時間。下面對節點的元素加以說明:

XML屬性 說明
drawable 當前幀引用的drawable資源
duration 當前幀顯示的時間(毫秒為單位)
oneshot 如果為true,表示動畫只播放一次停止在最后一幀上,如果設置為false表示動畫循環播放。
variablePadding If true, allows the drawable’s padding to change based on the current state that is selected.
visible 規定drawable的初始可見性,默認為flase;

下面就給個具體的XML例子,來定義一幀一幀的動畫:

<animation-list xmlns:android=”http://schemas.android.com/apk/res/android”
android:oneshot=”true”>
   <item android:drawable=”@drawable/rocket_thrust1″ android:duration=”200″ />
   <item android:drawable=”@drawable/rocket_thrust2″ android:duration=”200″ />
   <item android:drawable=”@drawable/rocket_thrust3″ android:duration=”200″ />
</animation-list>

上面的XML就定義了一個Frame Animation,其包含3幀動畫,3幀動畫中分別應用了drawable中的3張圖片:rocket_thrust1,rocket_thrust2,rocket_thrust3,每幀動畫持續200毫秒。

然后我們將以上XML保存在res/anim/文件夾下,命名為rocket_thrust.xml,顯示動畫的代碼,如下:在OnCreate()中增加如下代碼:


ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);


rocketImage.setBackgroundResource(R.anim.rocket_thrust);


rocketAnimation = (AnimationDrawable) rocketImage.getBackground();


最后還需要增加啟動動畫的代碼:


public boolean onTouchEvent(MotionEvent event) {


if (event.getAction() == MotionEvent.ACTION_DOWN) {


rocketAnimation.start();


return true;

 }

 return super.onTouchEvent(event);
}

代碼運行的結果想必大家應該就知道了(3張圖片按照順序的播放一次),不過有一點需要強調的是:啟動Frame Animation動畫的代碼rocketAnimation.start(); 不能在OnCreate()中,因為在OnCreate()中AnimationDrawable還沒有完全的與ImageView綁定,在OnCreate()中啟動動畫,就只能看到第一張圖片。
下面,閱讀Android SDK中對AnimationDrawable的介紹,有個簡單的了解:

AnimationDrawable

獲取、設置動畫的屬性  
int getDuration() 獲取動畫的時長
int getNumberOfFrames() 獲取動畫的幀數
boolean isOneShot()

 

 

Void setOneShot(boolean oneshot)

獲取oneshot屬性
設置oneshot屬性
void inflate(Resurce r,XmlPullParser p,
AttributeSet attrs)
 
增加、獲取幀動畫
Drawable getFrame(int index) 獲取某幀的Drawable資源
void addFrame(Drawable frame,int duration) 為當前動畫增加幀(資源,持續時長)
動畫控制
void start() 開始動畫
void run() 外界不能直接掉調用,使用start()替代
boolean  isRunning() 當前動畫是否在運行
void stop() 停止當前動畫

總結說明

Frame Animation的定義、使用比較簡單,在這里已經詳細介紹完了,更加深入的學習還是到Android SDK去仔細了解吧,在Android SDK中也包含很多這方面的例子程序。注:Frame Animation 的XML 文件中不定義 interpolator 屬性,因為定義它沒有任何意義。

 

一、AnimationSet的具體使用方法

       1.AnimationSet是Animation的子類;

       2.一個AnimationSet包含了一系列的Animation;

       3.針對AnimationSet設置一些Animation的常見屬性(如startOffset,duration等),可以被包含在AnimationSet當中的Animation集成;

例:一個AnimationSet中有兩個Animation,效果疊加

java代碼:

 
AnimationSet animationSet = new AnimationSet(true);  
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);  
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,  
        Animation.RELATIVE_TO_SELF,0.5f,  
        Animation.RELATIVE_TO_SELF,0.5f);  
rotateAnimation.setDuration(1000);  
animationSet.addAnimation(rotateAnimation);   
animationSet.addAnimation(alphaAnimation);  
image.startAnimation(animationSet);  
 

二、Interpolator的具體使用方法

       Interpolator定義了動畫變化的速率,在Animations框架當中定義了一下幾種Interpolator

AccelerateDecelerateInterpolator:在動畫開始與結束的地方速率改變比較慢,在中間的時候速率快。

              AccelerateInterpolator:在動畫開始的地方速率改變比較慢,然后開始加速

              CycleInterpolator:動畫循環播放特定的次數,速率改變沿著正弦曲線

              DecelerateInterpolator:在動畫開始的地方速率改變比較慢,然后開始減速

              LinearInterpolator:動畫以均勻的速率改變

例 在set標簽上:

xml代碼:

android:interpolator="@android:anim/accelerate_interpolator"  

如果一個set中包含了兩種動畫效果,要想這兩種動畫效果共享一個interpolator,可以在set標簽上添加:

xml代碼:

 

android:shareInterpolator="true"  

 

如果不想共享一個interpolator,則可以在alpha等標簽上添加。

另以上方法是在xml上處理interpolator,如果是在代碼上設置 共享一個interpolator,則可以在AnimationSet設置interpolator,如果不設置共享一個interpolator則可以 在alpha等的對象上面設置interpolator:

java代碼:

animationSet.setInterpolator(new AccelerateInterpolator());  

java代碼:

alphaAnimation.setInterpolator(new AccelerateInterpolator());  

 

 三、Frame-By-Frame Animations的使用方法

       Frame-By-Frame Animations是一幀一幀的格式顯示動畫效果。類似于電影膠片拍攝的手法。

例子程序:

       多張圖片展示一個人行走的動畫。

Main.xml

xml代碼:

 
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
    <LinearLayout  
        android:orientation="horizontal"  
        android:layout_height="wrap_content"   
        android:layout_width="wrap_content">  
        <Button   
            android:id="@+id/button"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:text="運動"/>  
    </LinearLayout>  
    <LinearLayout  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent">  
        <ImageView   
            android:id="@+id/image"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_centerInParent="true"/>     
    </LinearLayout>  
</LinearLayout>   
 

Anim.xml

xml代碼:

 

 
<?xml version="1.0" encoding="utf-8"?>  
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
    android:oneshot="false">  
    <item android:drawable="@drawable/a_01" android:duration="50"/>  
    <item android:drawable="@drawable/a_02" android:duration="50"/>  
    <item android:drawable="@drawable/a_03" android:duration="50"/>  
    <item android:drawable="@drawable/a_04" android:duration="50"/>  
    <item android:drawable="@drawable/a_05" android:duration="50"/>  
    <item android:drawable="@drawable/a_06" android:duration="50"/>  
</animation-list>  
 

 

AnimationsActivity.java

 

 
package com.android.activity;  
import android.app.Activity;  
import android.graphics.drawable.AnimationDrawable;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.ImageView;  
public class AnimationsActivity extends Activity {  
    private Button button = null;  
    private ImageView imageView = null;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        button = (Button)findViewById(R.id.button);  
        imageView = (ImageView)findViewById(R.id.image);  
        button.setOnClickListener(new ButtonListener());  
    }  
    class ButtonListener implements OnClickListener{  
        public void onClick(View v) {  
            imageView.setBackgroundResource(R.anim.anim);  
            AnimationDrawable animationDrawable = (AnimationDrawable)  
                imageView.getBackground();  
            animationDrawable.start();  
        }  
    }  
}  
 

看似十分完美,跟官方文檔上寫的一樣,然而當我們運行這個程序時會發現,它只停留在第一幀,并沒有出現我們期望的動畫,也許你會失望的說一 句:“Why?”,然后你把相應的代碼放在一個按鈕的點擊事件中,動畫就順利執行了,再移回到onCreate中,還是沒效果,這個時候估計你會氣急敗壞 的吼一句:“What the fuck!”。但是,什么原因呢?如何解決呢?

出現這種現象是因為當我們在onCreate中調用AnimationDrawable的start方法時,窗口Window對象還沒有完全初始 化,AnimationDrawable不能完全追加到窗口Window對象中,那么該怎么辦呢?我們需要把這段代碼放在 onWindowFocusChanged方法中,當Activity展示給用戶時,onWindowFocusChanged方法就會被調用,我們正是 在這個時候實現我們的動畫效果。當然,onWindowFocusChanged是在onCreate之后被調用的,如圖:

from:http://blog.csdn.net/liuhe688/article/details/6657776

運行結果:

一、LayoutAnimationsContrlller的使用方法

       LayoutAnimationsContrlller可以用于實現使多個控件按順序一個一個的顯示。

              1)LayoutAnimationsContrlller用于為一個layout里面的控件,或者是一個ViewGroup里面的控件設置動畫效果。

              2)每一個控件都有相同的動畫效果。

              3)控件的動畫效果可以在不同的時間顯示出來。

              4)LayoutAnimationsContrlller可以在xml文件當中設置,以可以在代碼當中進行設置。

二、ListView與Animaions結合使用

       1.在xml當中使用LayoutAnimationsController

              1)在res/anim文件夾下創建一個名為list_anim_layout.xml文件:

                     android:dylay - 動畫間隔時間;

                     android:animationOrder - 動畫執行的循序(normal:順序,random:隨機,reverse:反向顯示)

                     android:animation – 引用動畫效果文件

xml代碼:

<layoutAnimation   
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:delay="0.5"  
    android:animationOrder="normal"  
    android:animation="@anim/list_anim"/> 


2)在布局文件當中為ListVIew添加如下配置:

xml代碼:

 

android:layoutAnimation="@anim/list_anim_layout"  

 

完整代碼:


 List_anim_layout.xml

xml代碼:

<?xml version="1.0" encoding="utf-8"?>  
<layoutAnimation   
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:delay="0.5"  
    android:animationOrder="normal"  
    android:animation="@anim/list_anim"/>  

List_anim.xml

xml代碼:

 

 
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
    <ListView   
        android:id="@id/android:list"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:scrollbars="vertical"  
        android:layoutAnimation="@anim/list_anim_layout"/>  
    <Button   
        android:id="@+id/button"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="測試"/>  
</LinearLayout>  
 

 


Item.xml

xml代碼:

 

 
<?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="horizontal"   
    android:paddingLeft="10dip"  
    android:paddingRight="10dip"   
    android:paddingTop="1dip"  
    android:paddingBottom="1dip">  
    <TextView android:id="@+id/name"   
        android:layout_width="180dip"  
        android:layout_height="30dip"   
        android:textSize="5pt"  
        android:singleLine="true" />  
    <TextView android:id="@+id/sex"   
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"   
        android:textSize="5pt"   
        android:singleLine="true"/>  
</LinearLayout>   
 

 

AnimationsActivity.java

Java代碼:

 

 
package com.android.activity;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import android.app.ListActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.ListAdapter;  
import android.widget.ListView;  
import android.widget.SimpleAdapter;  
public class AnimationsActivity extends ListActivity {  
    private Button button = null;  
    private ListView listView = null;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        listView = getListView();  
        button = (Button)findViewById(R.id.button);  
        button.setOnClickListener(new ButtonListener());  
    }  
    private ListAdapter createListAdapter() {  
        List<HashMap<String,String>> list =   
            new ArrayList<HashMap<String,String>>();  
        HashMap<String,String> m1 = new HashMap<String,String>();  
        m1.put("name", "bauble");  
        m1.put("sex", "male");  
        HashMap<String,String> m2 = new HashMap<String,String>();  
        m2.put("name", "Allorry");  
        m2.put("sex", "male");  
        HashMap<String,String> m3 = new HashMap<String,String>();  
        m3.put("name", "Allotory");  
        m3.put("sex", "male");  
        HashMap<String,String> m4 = new HashMap<String,String>();  
        m4.put("name", "boolbe");  
        m4.put("sex", "male");  
        list.add(m1);  
        list.add(m2);  
        list.add(m3);  
        list.add(m4);  
        SimpleAdapter simpleAdapter = new SimpleAdapter(  
                this,list,R.layout.item,new String[]{"name","sex"},  
                new int[]{R.id.name,R.id.sex});  
        return simpleAdapter;  
    }  
    private class ButtonListener implements OnClickListener{  
        public void onClick(View v) {  
            listView.setAdapter(createListAdapter());  
        }  
    }  
}  
 

 

運行結果:每一個item都是淡入淡出的按順序顯示。

2.在代碼當中使用LayoutAnimationsController

       對于在代碼中使用LayoutAnimationsController,只不過去掉了list_anim_layout.xml這個文件,以及listview當中的

xml代碼:

android:layoutAnimation="@anim/list_anim_layout"  

這句。將animation的布局設置更改到了ButtonListener代碼當中進行。

       1) 創建一個Animation對象:可以通過裝載xml文件,或者是直接使用Animation的構造方法創建Animation對象;

java代碼:

Animation animation = (Animation)AnimationUtils.loadAnimation(  
    AnimationsActivity.this, R.anim.list_anim); 

2) 創建LayoutAnimationController對象:

java代碼:

 

LayoutAnimationController controller = new LayoutAnimationController(animation);  

 

3) 設置控件的顯示順序以及延遲時間:

java代碼:

 

controller.setOrder(LayoutAnimationController.ORDER_NORMAL);  
controller.setDelay(0.5f); 

 

4) 為ListView設置LayoutAnimationController屬性:

java代碼:

 

listView.setLayoutAnimation(controller);  

 

三、AnimationListener的使用方法

       1.AnimationListener是一個監聽器,該監聽器在動畫執行的各個階段會得到通知,從而調用相應的方法;

       2.AnimationListener主要包括如下三個方法:

              ·onAnimationEnd(Animation animation) - 當動畫結束時調用

              ·onAnimationRepeat(Animation animation) - 當動畫重復時調用

              ·onAniamtionStart(Animation animation) - 當動畫啟動時調用

實例:

Main.xml 

xml代碼:

 
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/layout"  
    android:orientation="vertical"   
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
    <Button android:id="@+id/addButton"   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"   
        android:layout_alignParentBottom="true"  
        android:text="添加圖片" />  
    <Button android:id="@+id/deleteButton"   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"   
        android:layout_above="@id/addButton"  
        android:text="刪除圖片" />  
    <ImageView android:id="@+id/image"  
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"  
        android:layout_centerInParent="true"   
        android:layout_marginTop="100dip"  
        android:src="@drawable/image" />  
</RelativeLayout>  
 

AnimationListenerActivity.java

java代碼:

 

 
package com.android.activity;  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup;  
import android.view.ViewGroup.LayoutParams;  
import android.view.animation.AlphaAnimation;  
import android.view.animation.Animation;  
import android.view.animation.Animation.AnimationListener;  
import android.widget.Button;  
import android.widget.ImageView;  
public class AnimationListenerActivity extends Activity {  
    private Button addButton = null;  
    private Button deleteButton = null;  
    private ImageView imageView = null;  
    private ViewGroup viewGroup = null;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        addButton = (Button)findViewById(R.id.addButton);  
        deleteButton = (Button)findViewById(R.id.deleteButton);  
        imageView = (ImageView)findViewById(R.id.image);  
        //LinearLayout下的一組控件  
        viewGroup = (ViewGroup)findViewById(R.id.layout);  
        addButton.setOnClickListener(new AddButtonListener());  
        deleteButton.setOnClickListener(new DeleteButtonListener());  
    }  
    private class AddButtonListener implements OnClickListener{  
        public void onClick(View v) {  
            //淡入  
            AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);  
            animation.setDuration(1000);  
            animation.setStartOffset(500);  
            //創建一個新的ImageView  
            ImageView newImageView = new ImageView(  
                AnimationListenerActivity.this);  
            newImageView.setImageResource(R.drawable.image);  
            viewGroup.addView(newImageView,  
                new LayoutParams(  
                    LayoutParams.FILL_PARENT,  
                    LayoutParams.WRAP_CONTENT));  
            newImageView.startAnimation(animation);  
        }  
    }  
    private class DeleteButtonListener implements OnClickListener{  
        public void onClick(View v) {  
            //淡出  
            AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);  
            animation.setDuration(1000);  
            animation.setStartOffset(500);  
            //為Aniamtion對象設置監聽器  
            animation.setAnimationListener(  
                new RemoveAnimationListener());  
            imageView.startAnimation(animation);  
        }  
    }  
    private class RemoveAnimationListener implements AnimationListener{  
        //動畫效果執行完時remove  
        public void onAnimationEnd(Animation animation) {  
            System.out.println("onAnimationEnd");  
            viewGroup.removeView(imageView);  
        }  
        public void onAnimationRepeat(Animation animation) {  
            System.out.println("onAnimationRepeat");  
        }  
        public void onAnimationStart(Animation animation) {  
            System.out.println("onAnimationStart");  
        }  
    }  
} 
 

 

 運行結果:

刪除時慢慢淡出,添加時慢慢淡入

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()