教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

Android培訓實戰(zhàn)教程之屬性動畫

更新時間:2015年12月28日11時57分 來源:傳智播客Android培訓學院 瀏覽次數:


 
什么是Android屬性動畫
    屬性動畫(Property Animation)系統(tǒng)是一個健壯的動畫框架系統(tǒng),它可以滿足你大部分動畫需求。不管動畫對象是否已經繪制到屏幕上,你都可以在動畫執(zhí)行過程中改變它任意的屬性值。一個屬性動畫會在一段特定長度的時間內改變一個屬性(一個對象中的字段)的值。你可以通過以下幾步定義一個動畫:指定你要執(zhí)行動畫的屬性,比如動畫對象(View)在屏幕上的位置,指定執(zhí)行時長,指定你希望的屬性的變化值。
 
同類技術對比:
·        補間動畫(Tween Animation)
a.  漸變動畫支持四種類型:平移(Translate)、旋轉(Rotate)、縮放(Scale)、不透明度(Alpha)。
b.  只是顯示的位置變動,View的實際位置未改變,表現為View移動到其他地方,點擊事件仍在原處才能響應。
c.  組合使用步驟較復雜。
d.  View Animation 也是指此動畫。
·        幀動畫(Frame Animation)
a.  用于生成連續(xù)的Gif效果圖。
b.  Drawable Animation也是指此動畫。
·        屬性動畫(Property Animation)
a.  支持對所有View能更新的屬性的動畫(需要屬性的setXxx()和getXxx())
b.  更改的是View實際的屬性,所以不會影響其在動畫執(zhí)行后所在位置的正常使用。
c.  Android3.0 (API11)及以后出現的功能,3.0之前的版本可使用github第三方開源庫nineoldandroids.jar進行支持。
 
屬性動畫組成部分、相關類介紹:
1. ObjectAnimator :對象動畫執(zhí)行類。
2. ValueAnimator :值動畫執(zhí)行類,常配合AnimatorUpdateListener使用。
3. PropertyValuesHolder : 屬性存儲器,為兩個執(zhí)行類提供更新多個屬性的功能。
4. Keyframe :為 PropertyValuesHolder提供多個關鍵幀的操作值。
5. AnimatorSet :一組動畫的執(zhí)行集合類:設置執(zhí)行的先后順序,時間等。
6. AnimatorUpdateListener :動畫更新監(jiān)聽。
7. AnimatorListener :動畫執(zhí)行監(jiān)聽,在動畫開始、重復、結束、取消時進行回調。
8. AnimatorInflater :加載屬性動畫的xml文件。
9. TypeEvaluator :類型估值,用于設置復雜的動畫操作屬性的值。
10.                     TimeInterpolator :時間插值,用于控制動畫執(zhí)行過程。
 
 
1. ObjectAnimator對象動畫執(zhí)行類
 
介紹:
1.  通過靜態(tài)方法ofInt、ofFloat、ofObject、ofPropertyValuesHolder 獲取類對象。
2.  根據屬性值類型選擇靜態(tài)方法,如view的setLeft(int left) 則選用ofInt方法, setY(float y)則選用ofFloat方法。
3.  同ValueAnimator一樣,可以進行串聯(lián)式使用,示例如下。
 
示例:
·         簡單示例:View的橫向移動
    
 
·         復合示例:View彈性落下然后彈起,執(zhí)行一次。
 
 
2. ValueAnimator 值動畫執(zhí)行類
 
介紹
1.  構造方法與ObjectAnimator類似。
2.  與ObjectAnimator的區(qū)別在于ValueAnimator構造函數的參數中不包含動畫“屬性”信息。
3.  優(yōu)點:結合動畫更新監(jiān)聽onAnimationUpdate使用,可以在回調中不斷更新View的多個屬性,使用起來更加靈活。
 
示例:
View向右下角移動:
 
 
3. PropertyValuesHolder 屬性存儲器
 
介紹:
為ValueAnimator提供多個操作屬性及相應的執(zhí)行參數:       
 
示例:
同時修改View多個屬性的動畫:
 
4. Keyframe 關鍵幀
 
介紹:
為 PropertyValuesHolder提供關鍵幀的操作值集合。
 
示例:
以下示例表示該PropertyValuesHolder進行的旋轉(rotation)動畫,在執(zhí)行時間在0%, 50%, 100%時,其旋轉角度分別為0°, 360°, 0°。動畫執(zhí)行過程中自動進行補間。表現為自旋360°后再轉回來。
 
 
5. AnimatorSet 執(zhí)行集合類
 
介紹:
1.  為多個屬性動畫提供播放順序控制(注意play,with,after,before的用法)
2.  AnimatorSet類與AnimationSet類不能搞混,AnimatorSet在3.0及以上版本中才有。3.0之前的版本可使用第三方開源庫nineoldandroids.jar進行支持,功能使用完全一致
 
示例:
以下示例動畫的播放順序為
1. 播放 bounceAnim.
2. 同時播放 squashAnim1, squashAnim2, stretchAnim1, stretchAnim2
3. 接著播放 bounceBackAnim.
4. 最后播放 fadeAnim.
詳細代碼參見:http://developer.android.com/samples/index.html
 
 
6. AnimatorUpdateListener 動畫更新監(jiān)聽
 
介紹:
1.     在動畫執(zhí)行過程中,每次更新都會調用該回調,可以在該回調中手動更新view的屬性。
2. 當調用的屬性方法中沒有進行View的重繪時,需要進行手動觸發(fā)重繪。設置AnimatorUpdateListener監(jiān)聽,并在onAnimationUpdate回調中執(zhí)行View的invalidate()方法。
 
示例:
 
1. 在回調中手動更新View對應屬性:
 
 
2. 在自定義View內部用于引發(fā)重繪
 
7. AnimatorListener 動畫執(zhí)行監(jiān)聽
 
介紹:
1.  實現AnimatorListener中的方法可在動畫執(zhí)行全程進行其他任務的回調執(zhí)行。
2.  也可以添加AnimatorListener的實現類AnimatorListenerAdapter,僅重寫需要的監(jiān)聽即可。
 
示例:
 
 
8. AnimatorInflater 動畫加載器
 
介紹:
1.  屬性動畫可以通過xml文件的形式加載。
2.  set標簽內的animator也可單獨使用。
3.  XML語法如下:
 
<set android:ordering=["together" ¦ "sequentially"]>
          <objectAnimator
              android:propertyName="string"
              android:duration="int"
              android:valueFrom="float ¦ int ¦ color"
              android:valueTo="float ¦ int ¦ color"
              android:startOffset="int"
              android:repeatCount="int"
              android:repeatMode=["repeat" ¦ "reverse"]
              android:valueType=["intType" ¦ "floatType"]/>
          <animator
              android:duration="int"
              android:valueFrom="float ¦ int ¦ color"
              android:valueTo="float ¦ int ¦ color"
              android:startOffset="int"
              android:repeatCount="int"
              android:repeatMode=["repeat" ¦ "reverse"]
              android:valueType=["intType" ¦ "floatType"]/>
          <set>
              ...
          </set> 
</set>
 
示例:
   xml文件:
 
 
9. TypeEvaluator  類型估值
 
 介紹:
1.  TypeEvaluator可傳入參數值的類型(本例為PointF)
2.  重寫函數public T evaluate(float fraction, T startValue, T endValue);實現不同需求值的計算。
3.  注意fraction的使用,fraction是從開始到結束的分度值0.0 -> 1.0
 
示例: 
 
10. TimeInterpolator 時間插值器
 
1.  幾種常見的插值器:
 
Interpolator對象 資源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 快速到達終點并超出一小步最后回到終點
 
2.  自定義插值器
a. 實現Interpolator(TimeInterpolator)接口
b. 重寫接口函數float getInterpolation(float input);
 
 
以上源代碼下載地址:http://pan.baidu.com/s/1mgFXOkK


本文版權歸傳智播客Android培訓學院所有,歡迎轉載,轉載請注明作者出處。謝謝!
作者:傳智播客Android培訓學院
首發(fā):http://oisangadgets.com/android/

0 分享到:
和我們在線交談!