Animation及其子類相關說明

 

一、 Animation代碼生成說明:

Animation是個抽象類。有5個子類AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation分別實現了平移、旋轉、縮放、Alpha變換、和以上動畫的組合變換動畫。

動畫類主要作用於View

 

主要方法:

initialize(int width, int height, int parentWidth, 

int parentHeight);

 

初始化動畫的尺寸。

width, heigth動畫自身的寬、高

parentWidth, parentHeight爲動畫組件父類的寬高

 

void applyTransformation(float interpolatedTime, 

Transformation t);

該方法指定了動畫每一幀的變換效果。

interpolatedTime: 動畫執行的進度。即Interpolator計算出的結果。是一個大於等於0、小於等於1的浮點數。

Transformation t: 爲每一幀變化的載體。主要有兩個屬性。AlphaMatrix

 

1、 AlhpaAnimation

繼承自Animation,實現了簡單的旋轉動畫。

構造函數:

·AlphaAnimation(Context context, AttributeSet attrs);

XML中構建AlhpaAnimation對象。

 

·AlphaAnimation(float fromAlpha, float toAlpha);

fromAlpha:開始時的Alpha值。取值範圍爲0—1

toAlpha:結束時的Alpha值。取值範圍爲0-1

 

 

2、 RotateAnimation

繼承自Animation,實現了簡單的旋轉動畫。

構造函數:

·RotateAnimation(Context context, AttributeSet attrs);

XML中構建AlhpaAnimation對象。

 

·RotateAnimation(float fromDegrees, float toDegrees);

fromDegrees開始時的角度。

toDegrees:結束時的角度。

注:軸心點座標pivotX, pivotY默認爲(0,0)即左上角。

 

·RotateAnimation(float fromDegrees, float toDegrees, 

float pivotX, float pivotY);

pivotX: 圍繞旋轉的軸心點X座標。

pivotY: 圍繞旋轉的軸心點Y座標。

注:以上兩個參數取值爲絕對座標。

 

·RotateAnimation(float fromDegrees, float toDegrees, 

int pivotXType, float pivotXValue,

int pivotYType, float pivotYValue);

pivotXType, pivotYType:分別指定了pivotXValue, pivotYValue的表示類

型。取值範圍爲:

Animation.ABSOLUTE:指定了其表示的類型爲絕對座標。

Animation.RELATIVE_TO_SELF:指定了其表示類型爲相對於自身的座標。 1.0即表示100%

Animation.RELATIVE_TO_PARENT:指定了其表示類型爲相對於父控件的座標。 1.0即表示100%p

 

3、 ScaleAnimation

繼承自Animation,實現了簡單的縮放動畫。

構造方法:

·ScaleAnimation(Context context, AttributeSet attrs);

XML中構建ScaleAnimation對象

 

ScaleAnimation(float fromX, float toX, float fromY, 

float toY);

fromX:爲開始時的橫向縮放比例。

toX:爲結束時橫向的縮放比例。

fromY:爲開始時縱向的縮放比例。

toY: 爲結束時縱向的縮放比例。

 

注:爲縮放軸心點爲0,0即左上角。

 

·ScaleAnimation(float fromX, float toX, float fromY, 

float toY, float pivotX, float pivotY);

pivotX, pivotY縮放的軸心點。

 

·ScaleAnimation(float fromX, float toX, float fromY, float toY, 

int pivotXType, float pivotXValue, 

int pivotYType, float pivotYValue);

 

pivotXType:軸心點x軸相對於組件的位置類型。

pivotXValue:軸心點x軸的座標。根據pivotXType代表不懂的含義。

pivotYType:軸心點y軸相對於組件的位置類型。

pivotYValue:軸心點y軸的座標。

 

4、 TranslateAnimation

繼承自Animation,實現了簡單的平移動畫。

構造方法:

·TranslateAnimation(Context context, AttributeSet atrrs);

XML中構建TranslateAnimation

 

·TranslateAnimation(float fromXDelta, float toXDelta, 

float fromYDelta, float toYDelta);

fromXDelta, fromYDelta分別爲開始時的X, Y座標。

toXDelta, toYDelta分別爲結束時的X, Y座標。

 

TranslateAnimation(int fromXType, float fromXValue, 

int toXType, float toXValue, 

int fromYType, float fromYValue, 

int toYType, float toYValue);

fromXType:開始時x軸相對於組件的位置類型。

fromXValue:開始時x軸的座標。根據fromXType代表不同的意義。

toXType:結束時x軸相對於組件的位置類型。

toXValue:結束時x軸的座標。根據toXType代表不同的意義。

fromYType:開始時y軸相對於組件的位置類型。

fromYValue:開始時y軸的座標。根據fromYType代表不同的意義。

toYType:結束時y軸相對於組件的位置類型。

toYValue:結束時y軸的座標。根據toYType代表不同的意義。

 

5、 AnimationSet

該類繼承自Animation,在實現了Animation的基礎上將各種動畫效果合併在一起。

 

構造方法:

·AnimationSet(Context context, AttributeSet attrs);

XML中構建AnimationSet

 

·AnimationSet(boolean shareInterpolator);

參數指定了整合後的各種動畫是否要使用同樣的Interpolator;

 

void addAnimtaion(Animation animation);

將設置好的動畫加入到AnimtaionSet中。

 

二、 Interpolator接口

該接口主要是來對動畫播放的時間進行控制。Animation在播放每一幀是會計算出當的進度,即當前已播放的百分比。然後將該值交給Interpolator進行一次處理,由Interpolator控制播放速度。

接口方法:

float getInterpolation (float input);

傳入當前播放的百分比。0爲開始,1爲結束。

 

實現該接口的類有9個。用以實現不同的播放速度。比如先快後慢、先慢後快等。

例如:

LinearInterpolator:實現動畫的線性比方(勻速播放)

AccelerateInterpolator:實現動畫的加速播放。該類有一個參數factor爲加

速因子。越打加速效果越明顯。

DecelerateInterpolator:實現了減速播放的效果。同樣該類也有一個減速因子factor,值越大減速效果越明顯。

AccelerateDecelerateInterpolator:實現了開始和結束時較慢。中間階段會先加速在減速。

當創建了Interpolator示例後可以通過

AnimationsetInterpolator(Interpolator i)來設置到動畫中。

 

三、 Animation XML生成說明

 

各類的XML屬性說明:

 

1、 AlphaAnimation

說明:實現alpha漸變,可以使界面逐漸消失或者逐漸顯現

XML標籤

<alpha 

android:fromAlpha=float

android:toAlpha=float 

/>

屬性:

fromeAlpha:開始的alpha值。取值0-1

toAlpha:結束的alpha值。取值0-1

 

TranslateAnimation

說明:實現位置移動漸變,需要指定移動的開始和結束座標。其中的屬性值支持以下三種格式的:

如果取值爲-100100,並且以%結束,說明是以自身爲參照點;如果取值-100100,並且以%p結束,說明這個百分比是以他父控件爲參照點;如果是一個float值並且沒有後綴,則是一個絕對的像素數值

 

XML標籤:

<translate 

android:fromX=string

    android:toX=string

    android:fromY=string

android:toY=string 

/>

屬性:

fromXDelta:開始時的X位置

toXDelta:結束時的X位置

fromYDelta:開始時的Y位置

toYDelta:結束時的Y位置

注:默認是以自己爲相對參照物。

 

2、 ScaleAnimation: 

說明:實現縮放漸變,可以指定縮放的參考點。如果將軸心點設置爲0, 0則以左上角爲參照點,向右下角縮放。

XML標籤

<scale 

android:fromXScale=float

    android:toXScale=float

    android:fromYScale=float

    android:toYScale=float

    android:pivotX=string

android:pivotY=string 

/>

屬性:

fromXScale:開始時X的尺寸,1.0表示不改變。

toXScale:結束時Y的尺寸,1.0表示不改變。

fromYScale:開始時X的尺寸,1.0表示不改變。

toYScale:結束時Y的尺寸,1.0表示不改變。

pivotX:軸心點的X座標。

pivotY:軸心點的Y座標。

 

4RotateAnimation:實現旋轉漸變,可以指定旋轉的參考點,默認值爲(00)左上角。

XML標籤:

<rotate 

android:fromDegrees=float

    android:toDegrees=float

    android:pivotX=string

    android:pivotY=string 

/>

屬性:

fromDegrees:開始的角度。

toDegrees:結束的角度。

pivotX:軸心點的X座標。是以像素爲單位。0, 0爲左上角。

pivotY:中心店的Y座標。是以像素爲單位。0, 0爲左上角。

 

5AnimationSet: 

說明:代表上面的漸變組合。

XML標籤

<set android:shareInterpolator=boolean>

</set>

屬性:

shareInterpolator:指定一個Interpolator(動畫播放曲線)。包含的所有動畫將共享此Interpolator。該值爲boolean類型

用法:

 

 

<set android:shareInterpolator=boolean>

<translate/>

<alpha/>

<interpolator/>

</set>

6Interpolator:

說明:定義了一個播放曲線,例如:先快後慢,先慢逐步加快,等等。可以

R.styleable中定義它的所有子類。例如:<CycleInterpolator>, <EaseInInterpolator>, 和 <EaseOutInterpolator>

 

7共有屬性:

說明:除了上<alpha>, <scale>, <translate>, <rotate>, <set>定義的屬性外他們還繼承了其父類的共有屬性:

duration:動畫的持續時間。以毫秒爲單位。

starOffset:動畫開始時的延遲時間。以毫秒爲單位。

fillBefore:如果爲true,即在動畫開始變換之前即開始。

fillAfter:如果爲true,即在動畫變化結束後動畫結束。

repeatCount:指定了動畫的循環次數。

repeatMode:指定了動畫的循環模式。其爲:restart:動畫結束後從頭開始播。reverse:動

動畫結束後從後往前播。

zAdjustment:指定了動畫運行時的z軸模式。其值爲:normal, top, or bottom

interpolator:指定動畫的變化曲線。可以引用R.anim裏定義好的一些值。引用方式爲

@android.R.anim/interpolatorName

例如:

interpolator=@android:anim/accelerate_interpolator;

8XML導入方式:

例如:

Animation animation = AinmationUtils.loadAnimation(this, 

R.anim.trals);

 

Animations的分類

1、 Tweened Animations漸變動畫、補間動畫

提供了旋轉、移動、伸展和淡出等效果

2、 Frame-by-Frame Animations

可以創建一個Drawable序列,這些Drawable可以按照指定的時間間歇一個一個的顯示。

 

 

Tweened Animations的分類

1、 Alpha:淡入淡出效果

2、 Scale:縮放效果

3、 Rotate:旋轉效果

4、 Translate:移動效果

 

Animations的第一種使用方法:

 

使用Tweened Animations的步驟

1、 創建一個AnimationSet對象

2、 根據需要創建相應的Animation對象

3、 根據軟件動畫的需求,爲Animation對象設置相應的數據

4、 將Animation對象添加到AnimationSet對象當中

5、 使用控件對象開始執行AnimationSet

 

Animation的四個子類:

AlphaAnimationTranslateAnimation、ScaleAnimation、RotateAnimation

 

private class RotateButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

//創建一個AnimationSet對象

AnimationSet animationSet = new AnimationSet(true);

//創建一個RotateAnimation對象

RotateAnimation rotateAnimation = new RotateAnimation(0, 360,

Animation.RELATIVE_TO_PARENT, 0.5f,

Animation.RELATIVE_TO_PARENT, 0.2f);////旋轉點的x座標爲0.5倍父控件的寬度,y座標爲0.2倍父控件的長度

//設置動畫執行的時間(單位:毫秒)

rotateAnimation.setDuration(5000);

//RotateAnimation對象添加到AnimationSet當中

animationSet.addAnimation(rotateAnimation);

//開始動畫

imageView.setAnimation(animationSet);

}

}

    private class ScaleButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

//創建一個AnimationSet對象

AnimationSet animationSet = new AnimationSet(true);

//創建一個ScaleAnimation對象

ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,

Animation.RELATIVE_TO_SELF, 0.5f,

Animation.RELATIVE_TO_SELF, 0.5f);

//設置動畫執行的時間(單位:毫秒)

scaleAnimation.setDuration(5000);

//ScaleAnimation對象添加到AnimationSet當中

animationSet.addAnimation(scaleAnimation);

//開始動畫

imageView.setAnimation(animationSet);

}

    }

    

    private class AlphaButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

//創建一個AnimationSet對象

AnimationSet animationSet = new AnimationSet(true);

//創建一個AlphaAnimation對象

AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

//設置動畫執行的時間(單位:毫秒)

alphaAnimation.setDuration(1000);

//AlphaAnimation對象添加到AnimationSet當中

animationSet.addAnimation(alphaAnimation);

//開始動畫

imageView.setAnimation(animationSet);

}

    }

    

    private class TranslateButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

//創建一個AnimationSet對象

AnimationSet animationSet = new AnimationSet(true);

//創建一個TranslateAnimation對象

TranslateAnimation translateAnimation = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0f,

Animation.RELATIVE_TO_SELF, 0.5f,

Animation.RELATIVE_TO_SELF, 0f,

Animation.RELATIVE_TO_SELF, 1.0f);

//設置動畫執行的時間(單位:毫秒)

translateAnimation.setDuration(5000);

//TranslateAnimation對象添加到AnimationSet當中

animationSet.addAnimation(translateAnimation);

//開始動畫

imageView.setAnimation(animationSet);

}

    }

 

 

Tween Animation的通用屬性:

1、 setDuration(long durationMills)

設置動畫持續時間(單位:毫秒)

2、 setFillAfter(Boolean fillAfter)

如果fillAfter的值爲true,則動畫執行後,控件將停留在執行結束的狀態

3、 setFillBefore(Boolean fillBefore)

如果fillBefore的值爲true,則動畫執行後,控件將回到動畫執行之前的狀態

4、 setStartOffSet(long startOffSet)

設置動畫執行之前的等待時間

5、 setRepeatCount(int repeatCount)

設置動畫重複執行的次數

 

 

 

 

Animations的第二種使用方法:

 

1、 在res文件夾下面新建一個名爲anim的文件夾

2、 創建xml文件,並首先加入set標籤,改標籤如下:

<set xmlns:android"http://schemas.android.com/apk/res/android"

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

</set>

在該標籤當中加入rotate, alpha, scale或者translate標籤

3、 在代碼當中使用AnimationUtils當中裝載xml文件,並生成Animation對象

 

 

 

 

 

 

 

Alphaxml文件編寫方法 alpha.xml

<<alpha android:fromAlpha"0.1"

android:toAlpha"0.0"

android:startOffset"500"

android:duration"500" />

 

代碼中:

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);

imageView.startAnimation(animation);

 

 

Rotatexml文件編寫方法 rotate.xml

<rotate android:fromDegrees="0"

android:toDegrees="+350"

android:pivotX="50%"

android:pivotY="50%"

android:duration="3000"/>

android:pivotX的值共有三種設置方法:

1、 android:pivoteX= “50”這種方法使用絕對位置定位

2、 android:pivoteX= “50%”這種方法相對於控件本身定位

3、 android:pivoteX= “50%p”這種方法相對於控件的父控件定位

 

translatexml文件編寫方法

<translate android:fromXDelta="50%"

android:toXDelta="100%"

android:fromYDelta="0%"

android:toYDelta="100%"

android:duration="2000" />

 

scalexml文件編寫方法

<scale android:fromXScale="1.0"

android:toXScale="0.0"

android:fromYScale="1.0"

android:toYScale="0.0"

android:pivotX="50%"

android:pivotY="50%"

android:duration="2000"/>

 

 

 

 

 

 

 

AnimationSet

1、 AnimationSetAnimation的子類

2、 一個AnimationSet包含了一系列的Animation

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

 

AnimationSet中加入多個動畫

//創建一個AnimationSet對象

AnimationSet animationSet = new AnimationSet(true);

//創建一個RotateAnimation對象

RotateAnimation rotateAnimation = new RotateAnimation(0, 360,

Animation.RELATIVE_TO_PARENT, 0.5f,

Animation.RELATIVE_TO_PARENT, 0.2f);////旋轉點的x座標爲0.5倍父控件的寬度,y座標爲0.2倍父控件的長度

//創建一個AlphaAnimation對象

AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

//AlphaAnimation對象添加到AnimationSet當中

animationSet.addAnimation(alphaAnimation);

//設置動畫執行的時間(單位:毫秒)

animationSet.setDuration(5000);

//RotateAnimation對象添加到AnimationSet當中

animationSet.addAnimation(rotateAnimation);

//開始動畫

imageView.setAnimation(animationSet);

 

也可以在加載佈局文件的時候,佈局文件裏面定義多個動畫效果。

 

 

什麼是Interpolator

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

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

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

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

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

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

 

xml裏面設置:android:interpolator= ….

在代碼裏設置: (AlphaAnimationAnimationSet)animation.setInterpolator(new DecelerateInterpolator());

 

 

 

 

Frame-By-Frame Animations的使用方法:

res/drawable當中創建一個xml文件(anim_nv.xml),用於定義Animations的動畫序列:

<animation-list  >

<item />

<item   />

</animation-list>

 

代碼裏:

imageView.setBackgroundResource(R.drawable.anim_nv);

AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();

animationDrawable.start();

 

 

 

什麼是LayoutAnimationController

1、 LayoutAnimationController用於爲一個layout裏面的控件,或者是一個ViewGroup裏面的控件設置動畫效果。

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

3、 這些控件的動畫效果在不同的時間顯示出來

4、 LayoutAnimationController可以在xml文件當中設置,也可以在代碼當中進行設置。

 

 

xml當中使用LayoutAnimationController

1、 在res/anim文件夾當中創建一個新文件,名爲list_anim_layout.xml文件:

<layoutAnimationxmlns:android= http://schemas.android.com/apk/res/android

android:delay= “0.5”

android:animationOrder= “random”

android:animation= “@anim/list_anim” />

2、 在佈局文件當中爲ListView添加如下配置:

android:layoutAnimation= “@anim/list_anim_layout”

 

 

在代碼當中使用LayoutAnimationController

1、 創建一個Animation對象:

可以通過裝載xml文件,或者直接使用Animation的構造函數創建Animation對象

Animation animation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_item);

2、 使用如下代碼創建LayoutAnimationController對象:

LayoutAnimationControllerlac = new LayoutAnimationController(animation);

3、 設置控件顯示的順序:

Lac.setOrder(LayoutAnimationController.ORDER_NORMAL);

4、 爲ListView設置LayoutAnimationController屬性:

listView.setLayoutAnimation(lac);

 

什麼是AnimationListener

1、 AnimationListener是一個監聽器

2、 該監聽器在動畫執行的各個階段會得到通知,從而調用相應的方法

3、 主要包含以下三個方法:

a) onAnimationEnd(Animation animation)

b) onAnimationRepeat(Animation animation)

c) onAnimationStart(Animation animation)

 

animation.setAnimationListener()

原文地址 http://wenku.baidu.com/link?url=UOs93PQ1YCT3U22yeRdU5_8Od66eNHt9WmWiDMWGWMvSzV24ZcyeIuYj0ntHhzAbzDmFbp4wBJ8QuXQlldCnkV4ep_YY56SWPtivWmKQHrC

http://wenku.baidu.com/link?url=3sDw7g_Cm2oerRI99wYnJepghY_bBE-Y6Sni3oU0HmBf-UKzJc7bsKfcTfeAXxQbrp983G19OHGRpG76GHfeuMAHPDqGuv_Nd7D-Ao9mU1O


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章