Android 动画之LayoutAnimation

来源:互联网 发布:淘宝现在做什么赚钱 编辑:程序博客网 时间:2024/05/30 23:02

Android动画系列:

  1. Android之补间动画详解
  2. Android 动画之帧动画
  3. Android 动画之LayoutAnimation
  4. Android 动画之属性动画(一)
  5. Android 动画之属性动画(二)
  6. Android 动画之属性动画(三)
  7. Android 动画之属性动画(四)
  8. Android 动画之LayoutTransition
  9. 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属性

xml属性 对应的方法 描述 android:delay setDelay(float) 动画播放的延迟时间 android:animationOrder setOrder(int) 子view播放动画的顺序 android:interpolator setInterpolator(Interpolator)/setIntepolator(Context, @InterpolatorRes int) 插值器 android:animation LayoutAnimationController(animation) 指定子view的动画


注: 动画播放的延迟时间的单位为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():执行动画

代码示例

  1. XML中定义

    1. 在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>
    2. 在 在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>
    3. 定义ListView

      <ListView    android:id="@+id/lv_content"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layoutAnimation="@anim/layout"></ListView>
  2. 代码中定义

       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,此效果才会出现。

参考资料

  1. Android Layout 布局动画的介绍
  2. Android动画三部曲之一 View Animation & LayoutAnimation
  3. Android的Animation之LayoutAnimation使用方法
0 0