Android学习之界面篇(三)LayoutAnimationController详细介绍

来源:互联网 发布:mac os10.13锐捷 编辑:程序博客网 时间:2024/05/20 03:07

在前面学习了Android学习之界面篇(一)Android Animation简单使用和 Android学习之界面篇(二)Android AnimationSet简单使用的简单使用,但是这些动态效果只适应一个控件,或者说多个控件同时执行一种效果。如果我们需要一个界面中的多个控件按照相同的动画方式但是每个控件完成该动画的时刻不同的话,就可采用本节讲的LayoutAnimationController来方便的完成了。

LayoutAnimationController介绍:

  1. LayoutAnimationController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果(即整个布局)
  2. 每一个控件都有相同的动画效果
  3. 这些控件的动画效果可在不同的时间显示出来

Android官方定义:

Class Overview


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的方法分别进行介绍:

  • xml配置

由于layout-animation是对于某一组控件的操作,就需要一个基本的动画来定义单个控件的动画。另外还可以定义动画的显示顺序和延迟:

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"android:delay="30%"android:animationOrder="reverse"android:animation="@anim/slide_right"/>
  1. android:delay表示动画播放的延时,既可以是百分比,也可以是float小数。

  2. android:animationOrder表示动画的播放顺序,有三个取值normal(顺序)、reverse(反序)、random(随机)。

  3. android:animation指向了子控件所要播放的动画。

将layout-animation应用到ViewGroup中,xml布局添加:
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);



本例实现界面如下:



0 0
原创粉丝点击