layoutAnimation动画

来源:互联网 发布:查开放房记录软件 编辑:程序博客网 时间:2024/06/04 19:15

动画效果:

使用方法:

1,通过xml设置:

    在listview或者gridview的布局中添加:

 android:layoutAnimation="@anim/layout_random_fade"
在res目录内添加anim文件夹创建layout_random_fade资源文件。

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"        android:delay="0.5"        android:animationOrder="normal"        android:animation="@anim/fade" />
其中delay表示动画延迟时间,animationOrder表示动画播放顺序(normal正序,reverse反序,random随机),animation表示子控件的动画效果,同样在anim文件夹内创建

<alpha xmlns:android="http://schemas.android.com/apk/res/android"       android:interpolator="@android:anim/accelerate_interpolator"       android:fromAlpha="0.0" android:toAlpha="1.0"       android:duration="@android:integer/config_longAnimTime" />
2,通过代码:

 Animation animation=AnimationUtils.loadAnimation(this, R.anim.fade);   //得到一个LayoutAnimationController对象;   LayoutAnimationController controller = new LayoutAnimationController(animation);   //设置控件显示的顺序;   controller.setOrder(LayoutAnimationController.ORDER_REVERSE);   //设置控件显示间隔时间;   controller.setDelay(0.3);   //为ListView设置LayoutAnimationController属性;   listView.setLayoutAnimation(controller);   listView.startLayoutAnimation();

3,自定义顺序,当前面三种播放顺序不是我们想要的时候,我就需要自定义view设置,LayoutAnimationController有一个方法getTransformedIndex(AnimationParameters params),返回值就是播放动画的顺序,并且方法是protected,可以去扩展。 然后通过回调接口将返回值回调回来自己去处理。