LayoutAnimator,这个动画是作用在viewGroup包含的子控件的。也就是给ViewGroup的子控件做动画。
比如给ListView中的item做动画。
下面看看LayoutAnimation动画在xml文件中的通用配置:
<layoutAnimation xmlns:android=“http://schemas.android.com/apk/res/android”
android:duration=“integer”
android:delay=“float”
android:animationOrder=[“normal”|“reverse”|“random”]
android:animation="@anim/animation_test">
<!--android:animationOrder="normal" 表示子元素的动画顺序,有三个选项,normal,reverse和random-->
<!--random,其中normal表示顺序显示,即排在前面的子元素先开始放入场动画-->
<!--reverse表示逆向开始,即排在后面的子元素先开始播放入场动画-->
<!--random表示子元素随机播放入场动画。-->
<!--android:animation="@anim/具体animation文件名称" 表示为子元素指定入场动画-->
下面具体给出一个使用layoutAnimation的示例: 1.首先创建一个子view的动画,在anim目录下,新建一个animation_item.xml的文件,其中的配置信息如下: <?xml version="1.0" encoding="utf-8"?>
<translate android:fromXDelta="0"
android:toXDelta="50"
android:fromYDelta="0"
android:toYDelta="50">
</translate>
2.创建layoutAnimation文件,在anim目录下,新建一个layout_animation_test.xml文件。配置信息如下: <?xml version="1.0" encoding="utf-8"?>
<!--android:animationOrder="normal" 表示子元素的动画顺序,有三个选项,normal,reverse和random-->
<!--random,其中normal表示顺序显示,即排在前面的子元素先开始放入场动画-->
<!--reverse表示逆向开始,即排在后面的子元素先开始播放入场动画-->
<!--random表示子元素随机播放入场动画。-->
<!--android:animation="@anim/animation_item" 表示为子元素指定入场动画-->
3.完成上面的layoutAnimation后,就可以使用了。下面这个示例是将layoutAnimation动画作用到ListView的item条目上。具体,只要设置android:layoutAnimation="@anim/layout_animation_test" 即可
进过这三步,就完成了给ListView的item添加动画的效果。
一般能够通过xml来配置动画效果的,代码也能实现,下面用代码方式完成上面步骤3的过程。
ListView lv = (ListView) findViewById(R.id.lv);
Animation layoutAnimation = AnimationUtils.loadAnimation(this,R.anim.animation_item);
LayoutAnimationController controller = new LayoutAnimationController(layoutAnimation);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
lv.setLayoutAnimation(controller);
lv.setAdapter(mAdapter);