Android Animations



animation有兩種方式來實現:1.代碼;2.xml


animation分兩類:
1.Tweened animations
2.frame-by-frame animations


tweened animations:
1.alpha:淡入淡出效果
2.scale:縮放效果
3.Rotate:旋轉效果
4.translate:移動效果


使用步驟:
1.創建一個AnimationSet對象。
AnimationSet animationSet = new AnimationSet(true);  
這裏的true是指讓set裏面的animation對象共享Interpolator。


2.根據需要創建對應的Animation對象。(四種子類,代表四種效果)
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);


3.根據軟件動畫的需求,爲Animation對象設置相應的數據。
alphaAnimation.setDuration(1000);


4.將Animation對象添加到AnimationSet對象中。
animationSet.addAnimation(alphaAnimation);


5.使用控件對象開始執行AnimationSet。
imageView.startAnimation(animationSet);




public RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
fromDegrees:開始角度。
toDegrees:結束角度
pivotXType:轉軸的x座標的類型
pivotXType:轉軸的x座標
pivotYType:轉軸的y座標的類型
pivotYType:轉軸的y座標


public AlphaAnimation (float fromAlpha, float toAlpha) 
fromAlpha:開始的可見度
toAlpha:結束的可見度


public ScaleAnimation (float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
fromX:開始大小(相對於自己)
toX:結束大小
fromY:開始大小
toY:結束大小
pivotXType:同上。這裏確定的點是圍繞哪一個點擴大或縮小


public TranslateAnimation (int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) 
分別設置的是開始和結束的位置。


Tween Animations通用屬性(Animation)
1.setDuration(long durationMills):設置動畫持續時間
2.setFillAfter(boolean fillAfter):true,則控件停留在動畫結束的狀態
3.setFillBefore(boolean fillBefore):true,則控件停留在動畫執行之前的狀態
4.setStartOffSet(long startOffSet):設置動畫執行之前的等待時間

5.setRepeatCount(int repeatCount):設置動畫重複執行的次數



=======================================================================================


XML 方式實現動畫


1.在res文件夾下新建一個anim的文件夾
2.創建xml文件,並首先加入set標籤,該標籤如下:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"></set>
3.在該標籤當中加入rotate,alpha,scale或者translate標籤
4.在代碼當中使用AnimationUtils當中裝在xml文件,並生成Animation對象。


代碼:
Animation animation = AnimationUtils.loadAnimation(AnimationTestActivity.this, R.anim.alpha);

imageView.startAnimation(animation);


<rotate
android:fromDegrees="0"
android:toDegrees="+350"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
/>
android:pivotX的值共有三種設置方法:
1.50,使用絕對爲指定爲
2.50%使用相對於控件本身定位
3.50%p 相對於父控件定位


<translate
android:fromXDelta="50%"  
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000"
/>
fromXDelta:起始位置
toXDelta:終止位置


<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"/>
fromXScale:開始。x軸的100%
toXScale:結束。x軸的0%,即全無。


=============================================================

1.AnimationSet是Animation的子類
2.一個AnimationSet包含了一系列的Animation
3.針對AnimationSet設置一些Animation常見的屬性,可以應用到AnimationSet裏的每一個Animation對象。


interpolator
它定義了動畫變化的速率。有如下幾種Interpolator:
AccelerateDecelerateInterpolator:在動畫開始與結束的地方速率改變比較慢,在中間的時候加速。
AccelerateInterpolator:在動畫開始的地方速率改變比較慢,然後開始加速。
CycleInterpolator:動畫循環播放待定的次數,速率改變沿着正弦曲線。
DecelerateInterpolator:在動畫開始的地方速率變化比較快,然後開始減速。
LinearInterpolator:動畫以勻速改變。


Frame-by-Frame方式:多幀動畫的形式
xml文件:animlist.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/xx" android:duration="500"/>
<item android:drawable="@drawable/yy" android:duration="500"/>
</animation-list>


代碼:
imageView.setBackgroundResource(R.drawable.animlist);
AnimationDrawable animationDrawable =(AnimationDrawable)imageView.getBackground();
animationDrawable.start(); //啓動動畫,默認狀態下會循環


========================================================================

LayoutAnimationController
1.用於爲一個layout裏面的控件或者一個ViewGroup裏面的控件設置動畫效果
2.每個控件都有相同的動畫效果
3.這些控件的動畫效果在不同的時間顯示出來。比如多個控件依次顯示
4.LayoutAnimationController既可以在xml文件中設置,也可以在代碼中設置


在xml中使用layoutAnimationController:
1.在res/anim中創建一個新文件,名字爲
list_anim_layout.xml:
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="random"  
android:aniamtion="@anim/list_anim"/>      這裏的list_anim是一個animationset的xml文件

order:radom,normal,reverse


2.在佈局文件當中爲ListView添加如下配置:
android:layoutAnimation="@anim/list_anim_layout"




在代碼中使用LayoutAnimationController
1.創建一個Animation對象。可以通過裝在xml文件,也可以直接使用Animation的構造函數創建Animation對象


2.創建LayoutAnimationController:
LayoutAnimationController lac = new layoutAnimationController(animation);


3.設置控件的顯示順序
lac.setOrder(LayoutAnnimationController.ORDER_NORMAL);


4.爲listview設置LayoutAnimationController屬性:
listView.setLayoutAnimation(lac);






AnimationListener
監聽動畫執行的各個階段。


主要有三個方法:
1.onAnimationEnd(Animation a);
2.onAnimationRepeat(Animation a);
3.onAnimationStart(Aniamtion a);

發佈了58 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章