LayoutAnimal 的簡單用法

關於這個動畫的原理我這裏不多講,我只是簡單介紹一下它的用法。

我將它分爲4大步,個人理解

step 1首先定義每個item的動畫效果 在res目錄下創建anim目錄創建以下文件list_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"android:shareInterpolator="true" >
      <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
      android:toAlpha="1.0" />
</set>

step2 爲ListView創建動畫文件list_anim_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:delay="2"
    android:animationOrder="normal"
    android:animation="@anim/list_anim"/>

step3 在佈局文件中爲ListView添加動畫

<!-- android:layoutAnimation="@anim/list_anim_layout"  這句話是添加列表控件的動畫,也可以在代碼中實現 -->
<ListView
      android:id="@id/android:list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:drawSelectorOnTop="false"
      android:scrollbars="vertical"
      android:layoutAnimation="@anim/list_anim_layout"/>

step4 在代碼中爲ListView添加動畫效果

// 在代碼中實現列表動畫
        Animation animation = (Animation) AnimationUtils.loadAnimation(
          mContext, R.anim.list_anim);
        LayoutAnimationController lac = new LayoutAnimationController(animation);
        lac.setDelay(0.4f);  //設置動畫間隔時間
        lac.setOrder(LayoutAnimationController.ORDER_NORMAL); //設置列表的顯示順序
        mListView.setLayoutAnimation(lac);  //爲ListView 添加動畫

就這樣


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