android LayoutAnimation的基本用法和说明

LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,这样当它的子元素 出场时都会具有这种动画效果。这种效果常常被用在ListView上,我们时常会看到一种特 殊的ListView,它的每个item都以一定的动画的形式出现,其实这并非什么高深的技术, 它使用的就是LayoutAnimation。LayoutAnimation也是一个View动画,为了给ViewGroup 的子元素加上出场效果,遵循如下几个步骤。
(1)定义LayoutAnimation,如下所示。

// res/anim/anim_layout.xml 
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" 
android:delay="0.5" 
android:animationOrder="normal" 
android:animation="@anim/anim_item"/>

它的属性的含义如下所示。
android:delay 表示子元素开始动画的时间延迟,比如子元素入场动画的时间周期为300ms,那么0.5 表示每个子元素都需要延迟150ms才能播放入场动画。总体来说,第一个子元素延迟 150ms开始播放入场动画,第2个子元素延迟300ms开始播放入场动画,依次类推。
android:animationOrder 表示子元素动画的顺序,有三种选项:normal、reverse和random,其中normal表示顺
序显示,即排在前面的子元素先开始播放入场动画;reverse表示逆向显示,即排在后面的 子元素先开始播放入场动画;random则是随机播放入场动画。
android:animation 为子元素指定具体的入场动画。
(2)为子元素指定具体的入场动画,如下所示。

// res/anim/anim_item.xml 
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
  android:duration="300" 
  android:interpolator="@android:anim/accelerate_interpolator" 
  android:shareInterpolator="true" >
 <alphaandroid:fromAlpha="0.0" android:toAlpha="1.0" />
 <translate android:fromXDelta="500"
   android:toXDelta="0" />
 </set>

(3)为ViewGroup指定android:layoutAnimation属性:android:layoutAnimation= “@anim/ anim_layout”。对于ListView来说,这样ListView的item就具有出场动画了,这种 方式适用于所有的ViewGroup,如下所示。

<ListView android:id="@+id/list" 
  android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layoutAnimation="@anim/anim_layout"
 android:background="#fff4f7f9"
 android:cacheColorHint="#00000000"
 android:divider="#dddbdb"
 android:dividerHeight="1.0px"
android:listSelector="@android:color/transparent" />

除了在XML中指定LayoutAnimation外,还可以通过LayoutAnimationController来实 现,具体代码如下所示。

ListView listView = (ListView) layout.findViewById(R.id.list); 
Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_ item);
 LayoutAnimationController controller = new LayoutAnimationController (animation); controller.setDelay(0.5f);
 controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
 listView.setLayoutAnimation(controller);
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北漂雨田

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值