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 添加动画

就这样


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