在前面学习了Android学习之界面篇(一)Android Animation简单使用和 Android学习之界面篇(二)Android AnimationSet简单使用的简单使用,但是这些动态效果只适应一个控件,或者说多个控件同时执行一种效果。如果我们需要一个界面中的多个控件按照相同的动画方式但是每个控件完成该动画的时刻不同的话,就可采用本节讲的LayoutAnimationController来方便的完成了。
LayoutAnimationController介绍:
Android官方定义:
A layout animation controller is used to animated a layout's, or a view group's, children. Each child uses the same animation but for every one of them, the animation starts at a different time. A layout animation controller is used by ViewGroup
to compute the delay by which each child's animation start must be offset. The delay is computed by using characteristics of each child, like its index in the view group. This standard implementation computes the delay by multiplying a fixed amount of miliseconds by the index of the child in its parent view group. Subclasses are supposed to override getDelayForView(android.view.View)
to implement a different way of computing the delay. For instance, aGridLayoutAnimationController
will compute the delay based on the column and row indices of the child in its parent view group. Information used to compute the animation delay of each child are stored in an instance of LayoutAnimationController.AnimationParameters
, itself stored in the ViewGroup.LayoutParams
of the view.
LayoutAnimationController可以在xml文件当中设置,也可以在代码中进行设置
本文就针对两种实现LayoutAnimationController的方法分别进行介绍:
由于layout-animation是对于某一组控件的操作,就需要一个基本的动画来定义单个控件的动画。另外还可以定义动画的显示顺序和延迟:
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="30%" android:animationOrder="reverse" android:animation="@anim/slide_right"/>
android:delay表示动画播放的延时,既可以是百分比,也可以是float小数。
android:animationOrder表示动画的播放顺序,有三个取值normal(顺序)、reverse(反序)、random(随机)。
android:animation指向了子控件所要播放的动画。
android:layoutAnimation=
"@anim/list_anim_layout"
这样在加载布局的时候就会自动播放layout-animtion。
LinearLayout rootView= (LinearLayout) findViewById(R.id.linearLayout); ScaleAnimation sa=new ScaleAnimation(0,1,0,1);//缩放效果 sa.setDuration(5000); LayoutAnimationController lac=new LayoutAnimationController(sa,2f);//dalay为延时 lac.setOrder(LayoutAnimationController.ORDER_RANDOM);//设置部件出现顺序 rootView.setLayoutAnimation(lac);
本例实现界面如下: