Android 动画之LayoutAnimation
来源:互联网 发布:淘宝现在做什么赚钱 编辑:程序博客网 时间:2024/05/30 23:02
Android动画系列:
- Android之补间动画详解
- Android 动画之帧动画
- Android 动画之LayoutAnimation
- Android 动画之属性动画(一)
- Android 动画之属性动画(二)
- Android 动画之属性动画(三)
- Android 动画之属性动画(四)
- Android 动画之LayoutTransition
- Android 动画之AnimatorSet
介绍
LayoutAnimation 是API Level 1 就已经有的,LayoutAnimation是对于ViewGroup控件所有的child view的操作,也就是说它是用来控制ViewGroup中所有的child view 显示的动画。比如,针对listView、gridView或者recyclerView,定义item的出场动画,不再是直来直去的显示,显得呆板。
一般对ListView使用layoutAnimation动画,对GridView使用gridLayoutAnimation动画。对RecyclerView来说,正常情况下只能使用layoutAnimation动画,应用gridLayoutAnimation动画的时候会报错,如果非要在RecyclerView中使用gridLayoutAnimation,那就辛苦下自定义一个吧。
常用API
XML属性
注: 动画播放的延迟时间的单位为s,假如设置为0.3f,相邻item的动画执行延迟时间为0.3s
关于子View的播放顺序:
android:animationOrder | setOrder | value | 描述
normal | LayoutAnimationController.ORDER_NORMAL | 0 | 按照item的顺序
reverse | LayoutAnimationController.ORDER_REVERSE | 1 | 按照item的倒序
random | LayoutAnimationController.ORDER_RANDOM | 2| 随机
LayoutAnimationController
- setAnimation(Animation animation):设置执行动画
- setAnimation(Context context, int resourceID):通过XML资源设置执行动画
- setDelay(float delay):设置相邻item之间动画延迟时间
- setInterpolator(Context context, int resourceID):通过XML资源设置内插器
- setInterpolator(Interpolator interpolator):设置内插器
- setOrder(int order):设置item的动画执行顺序
- start():执行动画
代码示例
XML中定义
在res/anim定义item执行的动画文件left.xml,动画效果为,从右侧移动至左侧,同时由透明逐渐显示。
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500"> <translate android:fromXDelta="100%" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="0" /> <alpha android:fromAlpha="0" android:toAlpha="1" /></set>
在 在res/anim定义layoutAnimtmation文件layout.xml,设置其执行的动画为left.xml,动画执行顺序为顺序执行。
<?xml version="1.0" encoding="utf-8"?><layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animation="@anim/left" android:animationOrder="normal" android:delay="30%" android:interpolator="@android:anim/linear_interpolator"></layoutAnimation>
定义ListView
<ListView android:id="@+id/lv_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layoutAnimation="@anim/layout"></ListView>
代码中定义
Animation animation = (Animation) AnimationUtils.loadAnimation( this, R.anim.left); LayoutAnimationController lac = new LayoutAnimationController(animation); lac.setDelay(0.4f); //设置动画间隔时间 lac.setOrder(LayoutAnimationController.ORDER_NORMAL); //设置列表的显示顺序 lvContent.setLayoutAnimation(lac);
注: 在XML中定义android:layoutAnimation=”@anim/layout”,仅在ListView第一次加载数据时,有效果。若后续调用适配器调用.notifyDataSetChanged(),动画效果不再呈现。即使调用getLayoutAnimation()获取ListView的LayoutAnimationController对象简称lac,然后lac调用start()方法重新执行动画效果,动画效果依然不会呈现,假如想每次数据更新时,都呈现此效果,只能够在每次调用notifyDataSetChanged()之前,重新设定LayoutAnimationController,即每次都进行代码定义LayoutAniamtion,此效果才会出现。
参考资料
- Android Layout 布局动画的介绍
- Android动画三部曲之一 View Animation & LayoutAnimation
- Android的Animation之LayoutAnimation使用方法
- Android 动画之LayoutAnimation
- Android动画之LayoutAnimation
- Android动画效果之:LayoutAnimation
- Android动画学习之LayoutAnimation三
- Android 动画之LayoutAnimation和LayoutTransition
- Android 布局动画之LayoutAnimation与LayoutTransition
- Android动画之layoutAnimation与gridLayoutAnimation
- Android动画--布局动画 LayoutAnimation
- Android动画--布局动画 LayoutAnimation
- Android的Animation之LayoutAnimation使用方法(控件设置动画)
- Android - LayoutAnimation 动画效果 - 示例
- android动画--layoutAnimation与gridLayoutAnimation
- LayoutAnimation动画
- layoutAnimation动画
- layoutAnimation动画
- Android动画三部曲之一 View Animation & LayoutAnimation
- Android-LayoutAnimation(布局动画)的学习
- Android中的动画(二)LayoutAnimation
- struts2 result详解
- iOS程序开发概览
- 非循环单链表
- NOIP2016提高A组集训第11场11.9 总结
- IntelliJ Idea 常用快捷键列表
- Android 动画之LayoutAnimation
- flex布局学习记录
- 396. Rotate Function
- 如果软件测试是门艺术,软件测试工程师就是艺术家 -- 浅析ZStack是如何做智能软件测试
- Android studio断点调试(全在这里)
- 搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程(参考)
- 洛谷 P1137 旅行计划
- IOS开发 POST请求数据 乱码问题
- C 笔记