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

本篇博客深入探讨Android的LayoutAnimationController,它允许为布局中的控件设置动画,使每个控件按不同时间呈现相同动画效果。内容涵盖LayoutAnimationController的基本概念、官方定义,以及如何通过XML和代码两种方式进行配置。示例展示了如何实现界面动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在前面学习了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);



本例实现界面如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值