android 動畫-Tween Animation(一)動畫體系簡介,涉及到的核心類、核心接口講解(上)

一、Tween Animation動畫概述
    Tween(補間)動畫可以對一個View執行一系列簡單變換(位置、大小、旋轉、透明度)。比如,你可以對一個ImageView進行改變大小、調整位置、放大縮小、調整透明度等變換。
    Tween動畫既可以通過xml來實現,也可以通過代碼來動態實現;寫在xml中調用會相對慢些,但是複用性強,內容清晰明瞭;寫在代碼中,內容不易讀,而且複用性差;
主要分爲四類:(類圖如下)

可以看到andorid.view.animation包下主要如下四類Animation:
TraslateAnimation:平移動畫
ScaleAnimation:縮放動畫
RotateAnimation:旋轉動畫
AlphaAnimation:透明度動畫

兩個核心類:
Animation:所有View動畫類的抽象基類
AnimationSet: set of animations的概念,是一組動畫的概念,是集合的概念。用於承載一組畫的容器。

二、介紹這四類主要的動畫和兩個核心類;
1、Animation是動畫類的基類,其中定義了一些動畫的基礎屬性,及控制動畫顯示的一些基礎的方法;常用的屬性和方法如下:
            類

xml屬性

java實現

解釋

Animation

android:duration

setDuration(long)

動畫持續時間,毫秒爲單位

android:fillAfter

setFillAfter(boolean)

控件動畫結束時是否保持動畫最後的狀態

android:fillBefore

setFillBefore(boolean)

控件動畫結束時是否還原到開始動畫前的狀態

android:fillEnabled

setFillEnabled(boolean)

android:fillBefore效果相同

android:interpolator

setInterpolator(Interpolator)

設定插值器(指定的動畫效果,譬如回彈等)

android:repeatCount

setRepeatCount(int)

重複次數

android:repeatMode

setRepeatMode(int)

重複類型有兩個值,reverse表示倒序回放,restart表示從頭播放

android:startOffset

setStartOffset(long)

調用start函數之後等待開始運行的時間,單位爲毫秒


2、AlphaAnimation extends Animation; AlphaAnimation的主要屬性都是通過構造函數傳遞,主要屬性如下表:

AlphaAnimation

android:fromAlpha

AlphaAnimation(float fromAlpha, ...) 

 

動畫開始的透明度,從0.0 --1.0 0.0表示全透明,1.0表示完全不透明

android:toAlpha

AlphaAnimation(...float toAlpha) 

動畫結束的透明度,從0.0 --1.0 0.0表示全透明,1.0表示完全不透明


3、TranslateAnimation extends Animation,TranslateAnimation中主要的屬性都是通過構造函數傳遞的;

這裏簡單解釋下TranslateAnimation的三個構造函數:

TranslateAnimation(Context context,AttributeSet attrs):這個構造函數是通過xml寫View動畫的時候系統回調的,我們不用管。

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue)

通過函數的參數我們可以發現XXXType和XXXValue是一一對應的,看字面意思是說,每個Value值可以對應不同Value類型,沒錯,Value類型不同,ValueType的寫法是不一樣的;在Animation中給我們提供了三種ValueType,分別是:

public static final int ABSOLUTE = 0;

public static final int RELATIVE_TO_SELF = 1;

public static final int RELATIVE_TO_PARENT 2;

通過傳入這三種value類型,具體的value的寫法也是不一樣的,實現效果也是不一樣的:
valueType
value內容 解釋
ABSOLUTE 
50
絕對的像素值
RELATIVE_TO_SELF 
50%
相對於自身寬高的設置百分比,大小:0.0-1.0 浮點數
RELATIVE_TO_PARENT 
50%p
相對於父控件的寬高設置百分比,大小0.0-1.0 浮點數

TranslateAnimation(float fromXValue, float toXValue, float fromYValue,  float toYValue):這個函數中相對於上一個函數的valueType傳值都是ABSOLUTE ,也就是傳入的值都必須是絕對的像素值;


具體的屬性如下:其中特別注意下四個屬性的參數類型;

TranslateAnimation

android:fromXDelta

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
        int fromYType, float fromYValue, int toYType, float toYValue)

起始點X軸座標,可以是數值、百分數、百分數三種樣式;比如 5050%50%p

android:toXDelta

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
        int fromYType, float fromYValue, int toYType, float toYValue)

結束點X座標,可以是數值、百分數、百分數三種樣式;比如 5050%50%p

android:fromYDelta

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
        int fromYType, float fromYValue, int toYType, float toYValue)

起始點Y軸從標,可以是數值、百分數、百分數三種樣式比如 5050%50%p

android:toYDelta

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
        int fromYType, float fromYValue, int toYType, float toYValue)

結束點Y軸座標可以是數值、百分數、百分數三種樣式;比如 5050%50%p


4、ScacleAnimation extends Animation;主要屬性還是通過構造函數來實現的,具體如下:


ScaleAnimation

android:fromXScale

ScaleAnimation(float fromXfloat toXfloat fromY, float toY,
        float pivotX,float pivotY

 起始的X方向上相對自身的縮放比例,浮點值,比如1.0代表自身無變化,0.5代表起始時縮小一倍,2.0代表放大一倍;

android:toXScale

ScaleAnimation(float fromXfloat toXfloat fromY, float toY,
        float pivotX,float pivotY

結尾的X方向上相對自身的縮放比例,浮點值;

android:fromYScale

ScaleAnimation(float fromXfloat toXfloat fromY, float toY,
        float pivotX,float pivotY

起始的Y方向上相對自身的縮放比例,浮點值,

android:toYScale   

ScaleAnimation(float fromXfloat toXfloat fromY, float toY,
        float pivotX,float pivotY

結尾的Y方向上相對自身的縮放比例,浮點值;

android:pivotX

ScaleAnimation(float fromX, float toXfloat fromY, float toY,
        int pivotXType, float pivotXValue, int pivotYType, float pivotYValue

 縮放起點X軸座標,可以是數值、百分數、百分數p三種樣式,比如 5050%50%p;當爲數值時,表示在當前控件的左上角加上50px,做爲起始縮放點;如果是50%,表示在當前控件的左上角加上自己寬度的50%做爲起始點;如果是50%p,那麼就是表示在當前控件的左上角加上父控件寬度的50%做爲起始點x軸座標。

android:pivotY

ScaleAnimation(float fromX, float toXfloat fromY, float toY,
        int pivotXType, float pivotXValue, int pivotYType, float pivotYValue

縮放起點Y軸座標,取值及意義跟android:pivotX一樣。


涉及到的屬性比較多,但是都比較好理解,相對麻煩點的地方就是縮放中心點的參數;這個參數還是有三種的賦值方式,具體
賦值的方式和含義與(3)中講解TranslateAnimation是一致的,有不懂的看上面吧。
5、RotateAnimation extends Animation;主要屬性還是通過構造函數來實現,具體內容如下:

RotateAnimation

android:fromDegrees

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
        int pivotYType, float pivotYValue)

 

開始旋轉的角度位置,正值代表順時針方向度數,負值代碼逆時針方向度數

android:toDegrees

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
        int pivotYType, float pivotYValue)

 

結束時旋轉到的角度位置,正值代表順時針方向度數,負值代碼逆時針方向度數

android:pivotX

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
        int pivotYType, float pivotYValue)

pivotXType默認ABSOLUTE

 縮放起點X軸座標,可以是數值、百分數、百分數p三種樣式,比如 5050%50%p

android:pivotY 

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
        int pivotYType, float pivotYValue)

pivotYType默認ABSOLUTE

 縮放起點Y軸座標,可以是數值、百分數、百分數p三種樣式,比如 5050%50%p

其中也涉及到了pivotX和pivotY的寫法和實現,具體解釋同ScacleAnimation 中的解釋,不再贅述。
6、AnimationSet類:
這個類是一個容器類,主要用來實現組合動畫;組合動畫的實現方式有兩種(單一動畫的實現方式也是這兩種);
(1)通過xml的方式實現;
xml放置在res/anim/目錄下,如果res文件下沒有anim目錄,就之間新建一個;
動畫類與xml標籤的對應關係如下表:
                                                                標籤對應

AnimationSet

<set></set>

AlphaAnimation

<alpha></alpha>

RotateAnimation

<rotate></rotate>

TranslateAnimation

<translate></translate>

ScalceAnimation

<scale></scale>


簡單模板:
xml實現
<set
    android:interpolator=""
    android:shareInterpolator="">

    <scale "通用屬性"+“專有屬性”/>

    <rotate "通用屬性"+“專有屬性”/>

    <alpha "通用屬性"+“專有屬性”/>

    <translate "通用屬性"+“專有屬性”/>

    <set>

        <scale/>
        
        ...

    </set>
</set>

    xml中涉及到的四類動畫的屬性我不再贅述,在上文均有提到,其中通用屬性是指Animation的屬性,專有屬性是指每個XXXAnimaiton自己的屬性;
    set中的屬性介紹下,set中一共兩個屬性,interpolator和shareInterpolator;interpolator屬性用於設置整個動畫集的插值器,shareInterpolator是這個插值器的開關,如果shareInterpolator=true,則整個動畫集的動畫都按照指定的插值器來進行動畫操作,如果shareInterpolator=false,則指定的動畫插值器將不起作用,可以再針對具體的動畫標籤來設置動畫插值器;至於動畫插值器是什麼,稍後補上,這裏先記住這兩個屬性及其關係即可。

java代碼實現;
   AnimationSet animtionSet = AnimationUtils.loadAnimation(context,R.anim.XXX);//加載xml中的動畫布局
   animationSet.setXXX();//爲動畫集設置公用的屬性,可以設置多個
   XXXview.startAnimation(animationSet);//啓動動畫

(2)通過代碼動態實現;
TranslateAnimation translate = new TranslateAnimation(xx,xx,xx,xx);//定義平移動畫
AlphaAnimation alpha = new AlpahAnimation(xx,xx);//定義透明度動畫   這裏只是隨意舉例子
AnimationSet animationSet = new Animation(boolean);//注意,這裏的true,false和xml中設置的shareInterpolator是等價的;

translate.setXXX();//設置translate動畫屬性
alpha.setXXX();//設置alpha動畫屬性
animationSet.setXXX();//設置動畫集通用的屬性
animationSet.addAnimation(translate);
animationSet.addAnimation(alpha);//具體動畫添加到集合中
XXXview.StartAnimation(animationSet);//啓動動畫

注意:動畫集中的動畫可以同時開始動畫,也可以先後開始動畫,具體按照哪種方式執行,分別通過在AnimationSet和XXXAnimation中執行setDuration方法和setStartOff方法控制即可,當然,也可以通過xml來設置這兩個屬性。

由於文章篇幅過長,其他未講到的內容請看下一節;android 動畫-Tween Animation(一)動畫體系簡介,涉及到的核心類、核心接口講解(下)

















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