Android LayoutAnimation使用及擴展

在Android中,最簡單的動畫就是補間動畫了。經過補間動畫,能夠對一個控件進行位移、縮放、旋轉、改變透明度等動畫。可是補間動畫只能對一個控件使用,若是要對某一組控件播放同樣的動畫的話,能夠考慮layout-animationjava

##LayoutAnimation layout-animation可由xml和代碼兩種方式配置:android

  • XML配置

因爲layout-animation是對於某一組控件的操做,就須要一個基本的動畫來定義單個控件的動畫。另外還能夠定義動畫的顯示順序和延遲:web

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:delay="30%"
        android:animationOrder="reverse"
        android:animation="@anim/slide_right" />

其中canvas

  1. android:delay表示動畫播放的延時,既能夠是百分比,也能夠是float小數。
  2. android:animationOrder表示動畫的播放順序,有三個取值normal(順序)、reverse(反序)、random(隨機)。
  3. android:animation指向了子控件所要播放的動畫。

上述步驟完成以後,就能夠將layout-animation應用到ViewGroup中,xml佈局添加下面一行就ok:api

android:layoutAnimation="@anim/list_anim_layout"

這樣在加載佈局的時候就會自動播放layout-animtion。dom

  • 代碼配置

若是在xml中文件已經寫好LayoutAnimation,可使用AnimationUtils直接加載:ide

AnimationUtils.loadLayoutAnimation(context, id)

另外還能夠手動java代碼編寫,如:佈局

<!-- lang:java -->
    //經過加載XML動畫設置文件來建立一個Animation對象;
   Animation animation=AnimationUtils.loadAnimation(this, R.anim.slide_right);

   //獲得一個LayoutAnimationController對象;
   LayoutAnimationController controller = new LayoutAnimationController(animation);

   //設置控件顯示的順序;
   controller.setOrder(LayoutAnimationController.ORDER_REVERSE);

   //設置控件顯示間隔時間;
   controller.setDelay(0.3);

   //爲ListView設置LayoutAnimationController屬性;
   listView.setLayoutAnimation(controller);
   listView.startLayoutAnimation();

經過代碼設置能夠達到一樣效果。動畫

##擴展 前幾天遇到一個需求,將動畫順序改成左上到右下角展開,例如Material Design中這個樣子。雖然一個個按順序播放頁能夠實現,但也太low了,就但願可以找一個簡便的方法。(PS. 我的崇尚簡約就是美this

仔細觀察,很明顯就是一個LayoutAnimation,可是LayoutAnimation默認只有三種順序,即順序逆序和隨機,不能知足需求。去翻翻源碼看它是怎麼實現的,有沒有提供方法自定義順序?結果翻到了一個LayoutAnimationController#getTransformedIndex(AnimationParameters params)方法,返回值就是播放動畫的順序。而且這個方法是protected的,明顯就是可由子類來擴展。既然找到了方法,那麼就去實現它:

/**
     * custom LayoutAnimationController for playing child animation
     * in any order.
     *
     */
public class CustomLayoutAnimationController extends LayoutAnimationController {

// 7 just lucky number
public static final int ORDER_CUSTOM  = 7;

private Callback onIndexListener;

public void setOnIndexListener(OnIndexListener onIndexListener) {
    this.onIndexListener = onIndexListener;
}

public CustomLayoutAnimationController(Animation anim) {
    super(anim);
}

public CustomLayoutAnimationController(Animation anim, float delay) {
    super(anim, delay);
}

public CustomLayoutAnimationController(Context context, AttributeSet attrs) {
    super(context, attrs);
}

/**
 * override method for custom play child view animation order
*/
protected int getTransformedIndex(AnimationParameters params) {
    if(getOrder() == ORDER_CUSTOM && onIndexListener != null) {
        return onIndexListener.onIndex(this, params.count, params.index);
    } else {
        return super.getTransformedIndex(params);
    }
}

/**
 * callback for get play animation order
 *
 */
public static interface Callback{
    
    public int onIndex(CustomLayoutAnimationController controller, int count, int index);
}

}

經過複寫getTransformedIndex方法,添加自定義順序ORDER_CUSTOM,讓callback自定義控件播放動畫的順序,便可以達到任何想要的效果。

##總結 Android在設計的時候,提供了很好的擴展性。各類組件均可以按需求擴展。可能用的更多的是仍是**View.onDraw(Canvas canvas)**方法,但不單單是view,對於其餘組件,也能夠隨意的擴展。不少時候,並不必定須要自定義不少空間、功能之類的,每每擴展一些簡單的組件就能達到本身想要的結果。

相關文章
相關標籤/搜索