【Android】ListView淡入动画效果的实现

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>

2.为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"/>

3.在布局文件中为ListView添加动画

<!-- android:layoutAnimation="@anim/list_anim_layout"  这句话是添加列表控件的动画,也可以在代码中实现 -->
    <ListView
      android:id="@id/android:list"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:drawSelectorOnTop="false"
      android:scrollbars="vertical" 
      android:layoutAnimation="@anim/list_anim_layout"/>

4.在代码中为ListView添加动画效果(我一般是在代码中添加,布局中设置的我来回刷新导致只能在第一次进入时有动画效果,直接在代码中设置就解决这个问题了,如果有发现原因的麻烦告知学习下),以下代码放在adapter刷新之前就行:

// 在代码中实现列表动画
        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 添加动画
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值