Android 屬性動畫(Property Animation)

概述

Property Animation是一個強大的動畫框架,它支持對任何對象進行動畫操作。你可以定義一個動畫讓對象的屬性隨着時間的變化而改變,不管動畫是否需要在屏幕上繪製出來。屬性動畫會在一個特定的時間內改變某個對象的屬性值。想要進行動畫操作,你需要指定對象動畫的屬性,如對象在屏幕上的位置,動畫的執行時間等。

Android Developer:http://developer.android.com/intl/zh-cn/guide/topics/graphics/prop-animation.html

相關屬性值

  • Duration:動畫持續時間,setDuration()
  • Time interpolation:插值器,setInterpolator()
  • Repeat count and behavior:動畫重複次數和重複方式,setRepeatCount()和setRepeatMode()
  • Animator sets:動畫集
  • Frame refresh delay:幀刷新頻率

相關API

Animator

創建屬性動畫的基類,該類通常不會被使用,一般通過繼承該類實現屬性動畫。

  • ValueAnimator:屬性動畫的主要時間引擎,主要計算屬性動畫的屬性值。屬性動畫主要由兩個功能組成:(1)計算各幀的屬性值;(2)爲指定對象設置計算的值。ValueAnimator主要負責功能1,如果想通過ValueAnimator實現功能2,需要爲ValueAnimator添加AnimatorUpdateListener監聽。
  • ObjectAnimator:可以通過爲指定對象的屬性執行動畫。
  • AnimatorSet:屬性集,執行組合動畫。

Evaluators

用於控制屬性動畫如何計算屬性值。

  • IntEvaluator:int類型默認Evaluator
  • FloatEvaluator:float類型默認Evaluator
  • ArgbEvaluator:16進制RGB顏色類型默認Evaluator
  • TypeEvaluator:Evaluator接口類,可以用於自定義Evaluator

Interpolators

差值器,在 Android 逐幀動畫&補間動畫 已經介紹。

ValueAnimator

使用ValueAnimator執行動畫有以下步驟:

  1. 通過ValueAnimator的ofInt,ofFloat,ofObject的靜態方法獲得ValueAnimator對象
  2. 設置動畫時間,重複方式,重複次數,插值器
  3. 添加AnimatorUpdateListener監聽器
  4. 開始動畫

如下代碼:

/**
* ValueAnimator演示
*/
public void showValueAnimator() {
     ValueAnimator animator = ValueAnimator.ofInt(1, 100);

     //設置動畫時間,重複方式,重複次數,插值器
     animator.setDuration(3000);
     animator.setRepeatMode(ValueAnimator.RESTART);
     animator.setRepeatCount(3);
     animator.setInterpolator(new AccelerateInterpolator());

     //添加監聽
     animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator animation) {
             //改變TextView顯示的值
             mTvShow.setText(animation.getAnimatedValue() + "");
         }
     });
     animator.start();
}

ObjectAnimator

ObjectAnimator繼承自ValueAnimator,它可以將ValueAnimator 計算出來的值直接設置到指定對象的屬性上。因此使用ObjectAnimator不需要像ValueAnimator通過註冊AnimatorUpdateListener實現動畫效果。

如下代碼:

/**
* ObjectAnimator演示
*/
public void showObjectAnimator() {
    //定義一個動畫平移TextView
      ObjectAnimator animAlpha = ObjectAnimator.ofFloat(mTvShow, "translationX", 0f, 100f);
      animAlpha.setDuration(3000);
      animAlpha.setRepeatMode(ValueAnimator.REVERSE);
      animAlpha.setRepeatCount(3);
      animAlpha.start();
}

在這裏需要說明,通過ObjectAnimator的ofInt,ofFloat,ofObject的靜態方法獲得對象時,需要指定具體的對象及對象屬性名。如上述代碼的TextView對象mTvShow及它的平移屬性translationX。

使用ObjectAnimator注意事項:
1. 指定對象的屬性必須添加setter方法,如translationX的setTranslationX,alpha的setAlpha。
2. 通過ofInt,ofFloat,ofObject創建對象時,一般情況下需要指定開始和結束的屬性值,如果只指定一個,默認爲結束屬性值。那麼此時需要給屬性值添加getter方法,通過調用getter方法將獲取到的值作爲開始屬性值
3. 如果需要顯示view的動畫效果,需要通過onAnimationUpdate方法調用invalidate()實現。

AnimatorSet

通過AnimatorSet實現組合動畫,如下代碼:

/**
* AnimatorSet演示
*/
public void showAnimatorSet() {
   ObjectAnimator animTrans = ObjectAnimator.ofFloat(mTvShow, "translationX", 0f, 300f);
   ObjectAnimator animRotation = ObjectAnimator.ofFloat(mTvShow, "rotation", 0f, 360f);
   ObjectAnimator animY = ObjectAnimator.ofFloat(mTvShow, "scaleY", 0.0f, 1.0f);
   ObjectAnimator animAlpha = ObjectAnimator.ofFloat(mTvShow, "alpha", 0.0f, 1.0f);

   AnimatorSet animatorSet = new AnimatorSet();

   //同時行動畫
//        animatorSet.playTogether(animTrans, animRotation, animAlpha);

   //順序執行
//        animatorSet.playSequentially(animTrans, animRotation, animAlpha);

   //自定義順序
   animatorSet.play(animAlpha).after(animTrans);
   animatorSet.play(animAlpha).before(animRotation);
   animatorSet.play(animRotation).with(animY);

   animatorSet.setDuration(3000);
   animatorSet.start();
 }

KeyFrame和PropertyValuesHolder介紹

KeyFrame

指定動畫關鍵幀,一個KeyFrame對象定義一個time/value鍵值對(即指定時間點的屬性動畫的屬性值)。

實現步驟:
1. 通過KeyFrame的靜態方法ofInt(), ofFloat(), ofObject()獲得keyFrame對象
2. 通過PropertyValuesHolder類的ofKeyframe()方法指定屬性的關鍵幀
3. 通過ObjectAnimator的ofPropertyValuesHolder()方法獲得ObjectAnimator對象

代碼如下:

/**
* 演示KeyFrame
*/
public void showKeyFrame() {
   Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
   Keyframe kf1 = Keyframe.ofFloat(0.5f, 300f);
   Keyframe kf2 = Keyframe.ofFloat(1f, 100f);
   PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
   ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(mTvShow, pvhRotation);
   rotationAnim.setDuration(5000);
   rotationAnim.start();
}

PropertyValuesHolder

PropertyValuesHolder主要用於實現一組動畫,使用方法同AnimatorSet類似。

代碼如下:

/**
 * 演示PropertyValuesHolder
 */
public void showPropertyValuesHolder() {
    PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("translationX", 0f, 300f);
    PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);
    ObjectAnimator.ofPropertyValuesHolder(mTvShow, pvhX, pvhY).setDuration(2000).start();
}

ViewPropertyAnimator

ViewPropertyAnimator用於對view執行動畫操作,使用起來更加方便,只需要調用view的animate()方法即可實現。
代碼如下:

/**
 * ViewPropertyAnimator演示
 */
public void showViewPropertyAnimator() {
    mTvShow.animate().setDuration(2000).alpha(1.0f).alpha(0.0f);
    //mTvShow.animate().setDuration(2000).rotation(300f);
}

xml文件定義屬性動畫

屬性動畫同幀動畫,補間動畫一樣可以通過xml文件實現。首先創建文件夾res/animator

xml標記說明:

  • ValueAnimator:<animator>
  • ObjectAnimator:<objectAnimator>
  • AnimatorSet:<set>

如下:

//ValueAnimator
<animator xmlns:android="http://schemas.android.com/apk/res/android"
          android:duration="1000"
          android:repeatCount="1"
          android:repeatMode="reverse">
    <propertyValuesHolder>
        <keyframe android:fraction="0" android:value="1"/>
        <keyframe android:fraction=".2" android:value=".4"/>
        <keyframe android:fraction="1" android:value="0"/>
    </propertyValuesHolder>
</animator>
//objectAnimator 
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:propertyName="scaleX"
    android:valueFrom="1.0"
    android:valueTo="2.0"
    android:valueType="floatType" >
</objectAnimator>
//AnimatorSet
<set android:ordering="sequentially">
    <set>
        <objectAnimator
            android:propertyName="x"
            android:duration="500"
            android:valueTo="400"
            android:valueType="intType"/>
        <objectAnimator
            android:propertyName="y"
            android:duration="500"
            android:valueTo="300"
            android:valueType="intType"/>
    </set>
    <objectAnimator
        android:propertyName="alpha"
        android:duration="500"
        android:valueTo="1f"/>
</set>

在代碼中調用:

/**
 * xml演示
 */
public void showXml() {
     ObjectAnimator set = (ObjectAnimator) AnimatorInflater.loadAnimator(this,
             R.animator.property_animator);
     set.setTarget(mTvShow);
     set.start();
}

源碼下載:http://download.csdn.net/detail/kaifa1321/9464492

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