Android的Tween動畫詳解

jopen 10年前發布 | 13K 次閱讀 Android Android開發 移動開發

一個Tween動畫將對于View對象的內容進行一系列簡單的轉換,在animation提供了所以關于Tween動畫的類,主要有四個常用的類,AlphaAnimation(透明度漸變),RotateAnimation(旋轉動畫),ScaleAnimation(圖片縮放動畫),TranslateAnimation(移動動畫),AnimationSet(一個動畫的集合類),以下是對常用動畫特效類的構造方法的作用和參數進行講解

(1) AlphaAnimation

public AlphaAnimation(float fromAlpha, float toAlpha)

fromAlpha -  開始時候的透明度,其中1表示完全不透明,0表示完全透明的 

toAlpha  結束時候的透明度

setDuration(long durationMillis)  設置動畫執行的時間

setFillAfter(boolean fillAfter)  設置為true時,動畫停在執行完后的效果,默認是執行完動畫回到剛開始的效果

setRepeatCount(int repeatCount) 設置動畫重復次數,repeatCount默認為0,即執行一次,為1時,即執行2次

setRepeatMode(int repeatMode)  設置動畫重復的模式,有Animation.REVERSE和Animation.RESTART兩種方式,默認為 Animation.RESTART,Animation.RESTART的意思就是說比如你設置重復次數為1,當執行完第一次動畫之后,回到動畫開始然后執行第二次動畫,而你設置Animation.REVERSE時候,比如你動畫是從不透明----->透明,執行完第一次動畫的時候,變為不透明,然后執行第二次動畫,他就從不透明到透明,不知道大家理解我的意思了沒?

我就介紹幾個常用的方法,其他的動畫也有上面的那些方法,然后等下介紹setInterpolator(Interpolator)方法

(2) RotateAnimation

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

 fromDegrees  動畫開始的角度

toDegrees  動畫結束的角度

pivotXValue, pivotYValue  繞著旋轉的中心點的X坐標和Y坐標

pivotXType, pivotYType 旋轉中心點的的相對關系類型,有三種animation.absolute,animation.relative_to_self,或 animation.relative_to_parent,animation.absolute絕對坐標類型,也就是相對O點的位置,animation.relative_to_self相對自己,自己視圖的左上角那個點為O點位置, animation.relative_to_parent相對父視圖左上角那個點為O點位置,即自己View所在的ViewGroup的位置

(3) ScaleAnimation

public ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 

float fromX, float toX  X軸方向從開始的大小到結束的大小

float fromY, float toY  Y軸方向從開始的大小到結束的大小

pivotXValue, pivotYValue  繞著縮放的中心點的X坐標和Y坐標

pivotXType, pivotYType    縮放中心點的的相對關系類型,有三種animation.absolute,animation.relative_to_self,或 animation.relative_to_parent,animation.absolute絕對坐標類型,也就是相對O點的位置,animation.relative_to_self相對自己,自己視圖的左上角那個點為O點位置, animation.relative_to_parent相對父視圖左上角那個點為O點位置,即自己View所在的ViewGroup的位置

(4) TranslateAnimation

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

fromXType  X軸上開始點相對類型

fromXValue  開始點的值

toXType    X軸上結束點相對類型

toXValue,  結束點的值

Y軸同理

(5) AnimationSet

這是一個動畫的集合類,可以設置多個動畫一起執行,比較簡單,我就不多介紹了

interpolator的解釋

interpolator定義一個動畫的變化率(the rate of change)。這使得基本的動畫效果(alpha, scale, translate, rotate)得以加速,減速,重復等。

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 子類,實現了不同的速度曲線,如下:

</tr>

</tr>

</tr>

</tr>

</tr> </tbody> </table> 上面介紹的是通過代碼構造的動畫,當然我們也能通過XML文件寫動畫,個人推薦使用XML文件

動畫放在res下的anim下,下面我來介紹用代碼生成和XML文件的方式

AlphaAnimation 代碼實現

    //構造透明變化動畫  
                Animation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);  
                //設置動畫執行時間  
                alphaAnimation.setDuration(2000);  
                //設置動畫重復方式  
                alphaAnimation.setRepeatMode(Animation.REVERSE);  
                //設置動畫重復次數  
                alphaAnimation.setRepeatCount(5);  
                //設置動畫變化率  
                alphaAnimation.setInterpolator(new AccelerateInterpolator());  
</div> </div> AlphaAnimation XML實現

    <?xml version="1.0" encoding="utf-8"?>  
    <alpha   xmlns:android="http://schemas.android.com/apk/res/android"  
        android:fromAlpha="1.0"  
        android:toAlpha="0.0"  
        android:duration="2000"  
        android:repeatCount="5"  
        android:repeatMode="reverse"  
        android:interpolator="@android:anim/accelerate_interpolator" />  
</div> </div> 通過AnimationUtils.loadAnimation(this, R.anim.alpha)就能拿到動畫了


RotateAnimation代碼實現

    Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  
                rotateAnimation.setDuration(2000);  
                rotateAnimation.setRepeatMode(Animation.RESTART);  
                rotateAnimation.setRepeatCount(5);  
                rotateAnimation.setInterpolator(new LinearInterpolator());  
</div> </div> RotateAnimation XML實現

    <?xml version="1.0" encoding="utf-8"?>  
    <rotate  xmlns:android="http://schemas.android.com/apk/res/android"  
        android:fromDegrees="0"  
        android:toDegrees="360"  
        android:pivotX="50%"  
        android:pivotY="50%"  
        android:duration="2000"  
        android:repeatCount="5"  
        android:repeatMode="reverse"  
        android:interpolator="@android:anim/linear_interpolator">  
    </rotate>  
</div> </div>

值得注意的地方是android:pivotX="50%",android:pivotY="50%"  當相對自己的是要加"%",相對父容器就不要加“%",這里就這個比較重要

 

RotateAnimation 動畫可以自定義圓形進度條,給個例子吧,用的是XML文件定義的

<?xml version="1.0" encoding="utf-8"?>  
<rotate xmlns:android="http://schemas.android.com/apk/res/android"  
    android:drawable="@drawable/loading_large6"  
    android:duration="1000"  
    android:fromDegrees="0"  
    android:interpolator="@android:anim/linear_interpolator"  
    android:pivotX="50%"  
    android:pivotY="50%"  
    android:repeatCount="infinite"  
    android:repeatMode="restart"  
    android:toDegrees="360" />  
</div> </div> 來自:http://blog.csdn.net/xiaanming/article/details/8997260

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
AccelerateDecelerateInterpolator 在動畫開始與介紹的地方速率改變比較慢,在中間的時候加速
AccelerateInterpolator 在動畫開始的地方速率改變比較慢,然后開始加速
CycleInterpolator 動畫循環播放特定的次數,速率改變沿著正弦曲線
DecelerateInterpolator 在動畫開始的地方速率改變比較慢,然后開始減速
LinearInterpolator 在動畫的以均勻的速率改變
  • sesese色