LayoutAnimation(ListView item入场动画)

    xiaoxiao2023-10-25  177

    LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,这样当它的子元素出场时都会具有这种动画效果。这种效果常常被用在ListView上,LayoutAnimation也是一个View动画,给ViewGroup的子元素加上出场效果,下面用ListView的item出场动画为例。

    定义LayoutAnimation

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

    android:delay 表示子元素开始动画的时间延迟,比如子元素入场动画的时间周期为300ms,那么0.5表示每个子元素都需要延迟150ms才能播放入场动画。总体来说,第一个子元素延迟150ms开始播放入场动画,第2个子元素延迟300ms开始播放入场动画

    android:animationOrder 表示子元素动画的顺序,有三种选项:normal、reverse和random,其中normal表示顺序显示,即排在前面的子元素先开始播放入场动画;reverse表示逆向显示,即排在后面的子元素先开始播放入场动画;random则是随机播放入场动画。

    android:animation 为子元素指定具体的入场动画。

    定义的animation动画 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" /> <translate android:fromXDelta="500" android:toXDelta="0" /> </set> 在xml中引用动画 <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" android:layoutAnimation="@anim/layout_animation"> </ListView> 在代码中定义 val animation = AnimationUtils.loadAnimation(this, R.anim.anim) val controller = LayoutAnimationController(animation) controller.delay = 0.5f controller.order = LayoutAnimationController.ORDER_NORMAL listView!!.layoutAnimation = controller

    最后效果图如下:

    最新回复(0)