LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,这样当它的子元素出场时都会具有该动画效果,这种效果通常被用在ListView中,下面以一个例子说明:
首先在anim中创建一个layoutanimation:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animationOrder="normal"
android:animation="@anim/list_anim"
android:delay="50%">
</layoutAnimation>
android:delay 子类动画时间间隔 (延迟),如果动画周期为1000ms,标示第一个动画会延迟500ms开始播放入场动画,第二个元素也是如此,以此类推。这里的50% 也可以是一个浮点数 如“0.5”等
android:animationOrder 表示子元素动画的顺序,有三种选项:normal,reverse和random,其中normal是默认选项,标示顺序显示;reverse 标示逆向显示;random则是随机播放入场动画
android:animation 表示显示时的具体动画,本例中动画如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0.5"
android:toAlpha="1"></alpha>
<translate
android:duration="1000"
android:fromXDelta="-200"
android:toXDelta="0" />
</set>
其中对ListView的设置如下:
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layoutAnimation="@anim/listview"
android:layout_height="match_parent"></ListView>
除了通过xml中指定的LayoutAnimation外,还可以通过LayoutAnimationController来实现,具体代码如下:
LayoutAnimationController control = AnimationUtils.loadLayoutAnimation(context, id);
//设置控件显示的顺序
control.setOrder(LayoutAnimationController.ORDER_REVERSE);
//设置控件显示间隔时间;
control.setDelay(1);
//为ListView设置LayoutAnimationController属性;
listView.setLayoutAnimation(lac);