LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,当它的子元素出场时都按照这个动画出场。
LayoutAnimation作用于viewgroup有两种方式:
1. 静态的使用xml文件实现。
2. 在代码中动态实现。
下面用ListView中的item设置动态出场效果来分别介绍两种方式:
静态的使用xml文件实现,分为三步
1. 在res的anim目录(res的文件夹下没有anim文件夹自己新建一个)下定义LayoutAnimation命名为anim_layout如下:
version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animation="@anim/anim_item"
android:animationOrder="normal"
>
其中的delay=“0.5”是指后一个item出场时间比前一个item的出场时间多0.5倍。
animationOrder指的是item的出场顺序是正常。
anim_item是指item出场的动画效果。
2. 在res的anim目录下定义LayoutAnimation命名为anim_item如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1"
/>
<translate
android:fromXDelta="500"
android:toXDelta="0"/>
</set>
1.在listview的布局中加入layoutAnimation。
<ListView
android:id="@+id/mylistView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@anim/anim_layout"
>
</ListView>
在代码中动态的实现,分为以下几步:
Animation animation= AnimationUtils.loadAnimation(this,R.anim.anim_item);
LayoutAnimationController controller=new LayoutAnimationController(animation);
controller.setDelay(0.5f);
listView.setLayoutAnimation(controller);
以上这篇LayoutAnimation给ListView中的item设置动态出场效果(实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。