Android的Tween動畫詳解
一個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 子類,實現了不同的速度曲線,如下:
AccelerateDecelerateInterpolator | 在動畫開始與介紹的地方速率改變比較慢,在中間的時候加速 | </tr>
AccelerateInterpolator | 在動畫開始的地方速率改變比較慢,然后開始加速 | </tr>
CycleInterpolator | 動畫循環播放特定的次數,速率改變沿著正弦曲線 | </tr>
DecelerateInterpolator | 在動畫開始的地方速率改變比較慢,然后開始減速 | </tr>
LinearInterpolator | 在動畫的以均勻的速率改變 | </tr> </tbody> </table> 上面介紹的是通過代碼構造的動畫,當然我們也能通過XML文件寫動畫,個人推薦使用XML文件