layoutAnimation作用于viewGroup,为ViewGroup制定一个动画,他的每个子元素都会按照这种动画出场。
首相定义LayoutAnimation如下所示:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="reverse"
android:animation="@anim/animation_scale" /> android:delay 表示子元素开始动画的时间延迟。
android:animationOrder 表示动画播放的顺序呢
normal:顺序显示
reverse:表示逆向显示
random:随机播放
android:animation 为每个子元素指定具体的入场动画,如下
<?xml version="1.0" encoding="utf-8"?>
<!-- 从0.1放大到1.5 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.1"
android:toXScale="1.5"
android:fromYScale="0.1"
android:toYScale="1.5"
android:duration="2000"
>
</scale>
最后为GroupView指定LayoutAnimation属性,以listview为例:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/xlv"
android:layoutAnimation="@anim/animation_layout"
android:background="#fff4f7f9"
android:divider="#ddd"
android:dividerHeight="1.0px"
android:listSelector="#999"
></ListView>除了在XML文件中进行设置,我们还可以使用代码来完成:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_scale);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
xlv.setLayoutAnimation(controller);效果如下
本文详细介绍了如何使用布局动画(LayoutAnimation)为ViewGroup指定动画,包括动画的延迟、顺序和具体入场动画的设置。以ListView为例,展示了如何在XML文件中和通过代码实现布局动画,并附上实际效果演示。
495

被折叠的 条评论
为什么被折叠?



