利用补间动画三分钟打造一个炫酷的ListView

本文介绍了如何利用LayoutAnimation为ViewGroup,如ListView,添加子元素出场的动画效果。详细讲解了LayoutAnimation的属性,如延迟时间、动画顺序和指定动画资源。通过简单的几步,就可以实现一个炫酷的ListView动画效果。

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

在上一篇View动画里,我们知道了View动画以及帧动画的简单使用,而这一篇主要是讲View动画的特殊使用场景,比如:
* 在ViewGroup中可以控制子元素的出场效果
* 在Activity中可以实现不同Activity之间的切换效果,

关于Activity切换这点,这篇帖子就不细说了无非就是overridePendingTransition的使用,本文主要要说的是LayoutAnimation

LayoutAnimatioon

LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,这样他的子元素出场时都会具有这种动画

LayoutAnimatioon中的属性:
  • android:delay=”0.5”
    表示子元素开始动画的延迟时间,比如子元素入场动画的时间周期为300ms,那么0.5表示每个子元素都需要延迟150ms才能播放入场动画,也就是说:第一个子元素延迟150ms,第二个子元素延迟300ms。
  • android:animationOrder=”normal”
    表示元素动画的顺序,有三种选项分别是:
    A:nromal–表示顺序显示
    B:reverse–表示逆向显示
    C:random–表示随机显示
  • android:animation=”@anim/anim_item”
    为子元素指定具体的入场动画
LayoutAnimatioon的使用遵循以下几个步骤:
  • 定义LayoutAnimation
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/anim_item"
    android:animationOrder="normal"
    android:delay="0.5">
</layoutAnimation>
  • 为子元素指定具体的入场动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="600"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true">
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1.0" />
    <translate
        android:fromXDelta="500"
        android:toXDelta="0" />
</set>
  • 为ViewGroup指定android:layoutAnimation属性:
在xml布局文件中指定android:layoutAnimation属性:
<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff4f7f9"
    android:cacheColorHint="#00000000"
    android:divider="#dddbdb"
    android:dividerHeight="1.0px"
    android:layoutAnimation="@anim/layout_animation"
    android:listSelector="@android:color/transparent"
/>
或者,可以在java代码中通过LayoutAnimationController来指定:
ListView listview = (ListView) findViewById(R.id.listview);
Animation animation = AnimationUtils.loadAnimation(TestAnimActivity.this, R.anim.anim_item);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
listview.setLayoutAnimation(controller);
  • 然后正常使用ViewGroup(比如ListView)即可

怎么样,如此简单就能做出一个炫酷的ListView特效,so easy!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值