相關文章:
一、概述
前兩篇,我爲大家講述了利用XML來定義動畫及插值器,但在代碼中,我們常常是動態生成動畫的,所以,這篇將爲大家講述如何用代碼生成動態生成動畫及插值器。
先簡單寫出各個標籤對應的類,方便大家理解:
- scale —— ScaleAnimation
- alpha —— AlphaAnimation
- rotate —— RotateAnimation
- translate —— TranslateAnimation
- set —— AnimationSet
二、Animation公共類
官方SDK講解頁面爲:《Animation》
第一篇中我們提到過,Animation類是所有動畫(scale、alpha、translate、rotate)的基類,它所具有的標籤及對應函數爲:
- android:duration setDuration(long) 動畫持續時間,以毫秒爲單位
- android:fillAfter setFillAfter(boolean) 如果設置爲true,控件動畫結束時,將保持動畫最後時的狀態
- android:fillBefore setFillBefore(boolean) 如果設置爲true,控件動畫結束時,還原到開始動畫前的狀態
- android:fillEnabled setFillEnabled(boolean) 與android:fillBefore 效果相同,都是在動畫結束時,將控件還原到初始化狀態
- android:repeatCount setRepeatCount(int) 重複次數
- android:repeatMode setRepeatMode(int) 重複類型,有reverse和restart兩個值,取值爲RESTART或 REVERSE,必須與repeatCount一起使用才能看到效果。因爲這裏的意義是重複的類型,即回放時的動作。
- android:interpolator setInterpolator(Interpolator) 設定插值器,其實就是指定的動作效果,比如彈跳效果等
在第一篇《 Animation 動畫詳解(一)——alpha、scale、translate、rotate、set的xml屬性及用法》 我們已經講解了每個標籤具體所具有的功能,這裏就不再細講,對於使用方法會在下面的各標籤中使用。
三、ScaleAnimation
這是scale標籤對應的類,官方SDK頁面爲:《ScaleAnimation》
在Scale標籤中,我們提到過它的自有屬性有下面幾條,先列一下:
- android:fromXScale 起始的X方向上相對自身的縮放比例,浮點值,比如1.0代表自身無變化,0.5代表起始時縮小一倍,2.0代表放大一倍;
- android:toXScale 結尾的X方向上相對自身的縮放比例,浮點值;
- android:fromYScale 起始的Y方向上相對自身的縮放比例,浮點值,
- android:toYScale 結尾的Y方向上相對自身的縮放比例,浮點值;
- android:pivotX 縮放起點X軸座標,可以是數值、百分數、百分數p 三種樣式,比如 50、50%、50%p,當爲數值時,表示在當前View的左上角,即原點處加上50px,做爲起始縮放點;如果是50%,表示在當前控件的左上角加上自己寬度的50%做爲起始點;如果是50%p,那麼就是表示在當前的左上角加上父控件寬度的50%做爲起始點x軸座標。(具體意義,後面會舉例演示)
- android:pivotY 縮放起點Y軸座標,取值及意義跟android:pivotX一樣。
- ScaleAnimation(Context context, AttributeSet attrs) 從XML文件加載動畫,基本用不到
- ScaleAnimation(float fromX, float toX, float fromY, float toY)
- ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
- ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
在標籤屬性android:pivotX中有三種取值,數,百分數,百分數p;體現在構造函數中,就是最後一個構造函數的pivotXType,它的取值有三個,Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT;
這三個構造函數難度不大,就不再細講,舉個例子說明:
在第一篇中Scale的例子的XML代碼爲:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50"
android:pivotY="50"
android:duration="700" />
對應的代碼構造代碼爲:
scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
scaleAnim.setDuration(700);
在控件使用的時候,同樣是使用:tv.startAnimation(scaleAnim);
四、AlphaAnimation
這是alpha標籤對就的類,官方SDK文檔地址是:《AlphaAnimation》同樣alpha標籤自有的屬性有:
- android:fromAlpha 動畫開始的透明度,從0.0 –1.0 ,0.0表示全透明,1.0表示完全不透明
- android:toAlpha 動畫結束時的透明度,也是從0.0 –1.0 ,0.0表示全透明,1.0表示完全不透明
- AlphaAnimation(Context context, AttributeSet attrs) 同樣,從本地XML加載動畫,基本不用
- AlphaAnimation(float fromAlpha, float toAlpha)
在第一篇文章中,我們構造的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.1"
android:duration="3000"
android:fillBefore="true">
</alpha>
如果用代碼構造同樣的效果,它所對應的代碼爲:alphaAnim = new AlphaAnimation(1.0f,0.1f);
alphaAnim.setDuration(3000);
alphaAnim.setFillBefore(true);
五、RotateAnimation
RotateAnimation類對應Rotate標籤,SDK文檔地址:《RotateAnimation》
Rotate標籤所具有的XML屬性有:
- android:fromDegrees 開始旋轉的角度位置,正值代表順時針方向度數,負值代碼逆時針方向度數
- android:toDegrees 結束時旋轉到的角度位置,正值代表順時針方向度數,負值代碼逆時針方向度數
- android:pivotX 縮放起點X軸座標,可以是數值、百分數、百分數p 三種樣式,比如 50、50%、50%p,具體意義已在scale標籤中講述,這裏就不再重講
- android:pivotY 縮放起點Y軸座標,可以是數值、百分數、百分數p 三種樣式,比如 50、50%、50%p
- RotateAnimation(Context context, AttributeSet attrs) 從本地XML文檔加載動畫,同樣,基本不用
- RotateAnimation(float fromDegrees, float toDegrees)
- RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
- RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
RotateAnimation跟ScaleAnimation差不多,關鍵問題同樣是pivotXType和pivotYType的選擇,同樣有三個取值:Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT;
根據每一篇中的XML寫出對應的JAVA構造代碼:
XML爲:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-650"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
android:fillAfter="true">
</rotate>
對應JAVA構造代碼爲:rotateAnim = new RotateAnimation(0, -650, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(3000);
rotateAnim.setFillAfter(true);
六、TranslateAnimation
很顯示TranslateAnimation類對應translate標籤,它的SDK官方文檔地址爲:《TranslateAnimation》translate標籤所具有的屬性爲:
- android:fromXDelta 起始點X軸座標,可以是數值、百分數、百分數p 三種樣式,比如 50、50%、50%p,具體意義已在scale標籤中講述,這裏就不再重講
- android:fromYDelta 起始點Y軸從標,可以是數值、百分數、百分數p 三種樣式;
- android:toXDelta 結束點X軸座標
- android:toYDelta 結束點Y軸座標
- TranslateAnimation(Context context, AttributeSet attrs) 同樣,基本不用
- TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
- TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
由於fromXDelta、fromYDelta、toXDelta、toYDelta這三個屬性都具有三種狀態,所以在構造函數中,最理想的狀態就是第三個構造函數,能夠指定每個值的類型,第二個構造函數:TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)使用是絕對數值。只有最後一個構造函數可以指定百分數和相對父控件的百分數。
下面以第一篇中的XML代碼爲例,用JAVA代碼構造同樣的效果:
XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="-80"
android:fromYDelta="0"
android:toYDelta="-80"
android:duration="2000"
android:fillBefore="true">
</translate>
對應的JAVA代碼爲:translateAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80,
Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80);
translateAnim.setDuration(2000);
translateAnim.setFillBefore(true);
七:AnimationSet
AnimationSet類對應set標籤,定義動作類的集合,對應的SDK文檔地址爲:《AnimationSet》它自己是沒有XML屬性的,所以我們直接說它的構造函數:
- AnimationSet(Context context, AttributeSet attrs) 同樣,基本不用
- AnimationSet(boolean shareInterpolator) shareInterpolator取值true或false,取true時,指在AnimationSet中定義一個插值器(interpolater),它下面的所有動畫共同。如果設爲false,則表示它下面的動畫自己定義各自的插值器。
增加動畫的函數爲:(更多函數,請參看SDK文檔)
- public void addAnimation (Animation a)
下面在第一篇中的XML代碼爲例寫出能構造同樣效果的JAVA代碼:
XML代碼爲:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fillAfter="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
<scale
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"/>
<rotate
android:fromDegrees="0"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
對應的JAVA代碼爲:alphaAnim = new AlphaAnimation(1.0f,0.1f);
scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
setAnim=new AnimationSet(true);
setAnim.addAnimation(alphaAnim);
setAnim.addAnimation(scaleAnim);
setAnim.addAnimation(rotateAnim);
setAnim.setDuration(3000);
setAnim.setFillAfter(true);
八、Interpolater插值器
關於插值器的效果及應用,我們專門開了一篇來講,看這裏:《Animation動畫詳解(二)——Interpolator插值器》關於插值器的SDK講解見《Animation Resources》中的Interpolators部分;
插值器XML屬性及對應的類如下表所示:
Interpolator class | Resource ID |
---|---|
AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator |
AccelerateInterpolator | @android:anim/accelerate_interpolator |
AnticipateInterpolator | @android:anim/anticipate_interpolator |
AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator |
BounceInterpolator | @android:anim/bounce_interpolator |
CycleInterpolator | @android:anim/cycle_interpolator |
DecelerateInterpolator | @android:anim/decelerate_interpolator |
LinearInterpolator | @android:anim/linear_interpolator |
OvershootInterpolator | @android:anim/overshoot_interpolator |
使用方法:(爲sacleAnimation增加bounce插值器)
ScaleAnimation interpolateScaleAnim=new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
interpolateScaleAnim.setInterpolator(new BounceInterpolator());
interpolateScaleAnim.setDuration(3000);
九、示例,源碼
下面我把上面所有的代碼集合到一個例子中,供大家下載;效果圖如下:
源碼下載地址:http://download.csdn.net/detail/harvic880925/8047669
請大家尊重原創者版權,轉載請標明出處:http://blog.csdn.net/harvic880925/article/details/40117115 謝謝!
如果我的文章有幫到您,記得加關注哦!