RecycleView 入场动画

作者: Android_冯星 | 来源:发表于2019-04-01 09:54 被阅读37次

    Enter animation using RecyclerView and LayoutAnimation Part 1: Lists

    1-Y7zsNHV3Y9PnZ1uayYMsMg.gif

    介绍

    本教程我们将带大家学习一个为RecyclerView添加初始动画的简单方法。实现这种动画有几种办法,比如:

    • 实现一个自定义的ItemAnimator

    • 在Adapter的onBindViewHolder()方法中添加动画

    我将采用第三种办法:LayoutAnimation。它非常简单,而且只需很少的代码。值得注意的是虽然这篇文章是用RecyclerView来做的,但是LayoutAnimation可以用在任何ViewGroup上。

    本文将讲述:

    1. 为每个item定义一个动画

    2. 使用item的动画定义LayoutAnimation

    3. 在XML或者代码中应用LayoutAnimation

    这是这个系列教程的第一篇,考虑的是RecyclerView为列表的场景(LinearLayoutManager)。虽然这里的方法用在GridLayoutManager的时候仍然有效,但是我们想为grid使用一个体验更好的动画,留在第二部分讲解。

    https://medium.com/@patrick_iv/enter-animation-using-recyclerview-and-layoutanimation-part-2-grids-688829b1d29b

    本教程的demo源码在这里,包含了List和Grid的例子:

    https://github.com/patrick-iv/Enter-animation-demo

    demo的apk在这里!

    让我们开始吧

    首先让我们创建item的动画,这里我们创建的是一个下落的动画:

    1-lx2_ejIZz7bOhgw9ewKFsA.gif

    在res/anim/目录下创建一个 item_animation_fall_down.xml 文件并添加如下内容:

    
     <set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="@integer/anim_duration_medium">
    
    <translate
     android:fromYDelta="-20%"
      android:toYDelta="0"
      android:interpolator="@android:anim/decelerate_interpolator"
      />
    
      <alpha
      android:fromAlpha="0"
      android:toAlpha="1"
      android:interpolator="@android:anim/decelerate_interpolator"
      />
    
      <scale
      android:fromXScale="105%"
      android:fromYScale="105%"
      android:toXScale="100%"
      android:toYScale="100%"
      android:pivotX="50%"
      android:pivotY="50%"
      android:interpolator="@android:anim/decelerate_interpolator"
      />
    
      </set>
    

    上面定义的动画元素将会同时运行,这里是对每个动画元素的解释:

    • Translate Y -20% to 0%
      在动画开始前,把view向上移动自身高度的20%,然后让他下降到自己最终的位置。

    • Alpha 0 to 1
      从完全不可见慢慢过渡到完全可见。

    • Scale X/Y 105% to 100%
      放大到105%,然后缩小的实际大小。

    定义LayoutAnimation

    定义了item要运行的动画之后,我们来定义把动画应用到每个子view的layout animation。在 res/anim/ 新建一个名为layout_animation_fall_down.xml 的文件,添加如下代码:

    
      <?xml version="1.0" encoding="utf-8"?>
      <layoutAnimation
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:animation="@anim/item_animation_fall_down"
      android:delay="15%"
      android:animationOrder="normal"
      />
    
    • android:animation="@anim/item_animation_fall_down”
      定义item运行的动画(资源)。

    • android:delay=”15%"
      为每个item动画添加一个延时,基于item动画的duration。0%将导致所有item同步运行,100%则让前一个item的动画运行完了下一个item才开始动画。我们这里使用的是15%,表示item A的动画运行了15%之后才开始运行B的动画。

    • android:animationOrder="normal"
      有三种选择:normal, reverse 和 random。它可以控制布局元素运行动画的顺序。Normal是按照布局的自然顺序( vertical: top to bottom, horizontal: left to right),Reverse跟Normal是相反的,Random则按照随即的顺序。

    使用 LayoutAnimation

    应用LayoutAnimation可以通过代码实现,也可以通过XML实现。

    java代码

     int resId = R.anim.layout_animation_fall_down;
     LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(ctx, resId);
     recyclerview.setLayoutAnimation(animation);
    

    xml

    
    1.  <android.support.v7.widget.RecyclerView
    2.  android:layout_width="match_parent"
    3.  android:layout_height="match_parent"                                        
    4.  android:layoutAnimation="@anim/layout_animation_fall_down"
    5.  />
    

    如果你想在数据变化的时候触发动画:

     private void runLayoutAnimation(final RecyclerView recyclerView) {
     final Context context = recyclerView.getContext();
     final LayoutAnimationController controller =
      AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_fall_down);
    
      recyclerView.setLayoutAnimation(controller);
      recyclerView.getAdapter().notifyDataSetChanged();
      recyclerView.scheduleLayoutAnimation();
      }
    

    Wrapping up

    内容就是这么多了。我觉得这是RecylerView从空到渲染完内容时执行动画的最佳方式。就如我刚才提到的,这种办法适用于所有ViewGroup。

    第二部分 讲解在RecyclerView为grid的时候如何对animation order 进行更多的控制,文章在这里:

    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/0819/8397.html

    别忘了本教程两个部分的文章的代码都在这里

    最后附两个动画:

    附: Slide from right

    item_animation_from_right.xml

      <?xml version="1.0" encoding="utf-8"?>
      <set xmlns:android="http://schemas.android.com/apk/res/android"
      android:duration="@integer/anim_duration_long">
      <translate
      android:interpolator="@android:anim/decelerate_interpolator"
      android:fromXDelta="100%p"
      android:toXDelta="0"
      />
    
      <alpha
      android:fromAlpha="0.5"
      android:toAlpha="1"
      android:interpolator="@android:anim/accelerate_decelerate_interpolator"
      />
    
      </set>
    

    layout_animation_slide_right.xml

     <?xml version="1.0" encoding="utf-8"?>
      <layoutAnimation
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:animation="@anim/item_animation_from_right"
      android:delay="10%"
      android:animationOrder="normal"
      />
    

    这里要注意的是android:fromXDelta="100%p"中的p 不是笔误。当添加了p之后百分比就是基于parent的,所以这里其实代表向右移动parent的整个宽度。

    附: Slide from bottom

    item_animation_from_bottom.xml

      <?xml version="1.0" encoding="utf-8"?>
      <set xmlns:android="http://schemas.android.com/apk/res/android"
      android:duration="@integer/anim_duration_long">
      <translate
      android:interpolator="@android:anim/accelerate_decelerate_interpolator"
      android:fromYDelta="50%p"
      android:toYDelta="0"
      />
    
      <alpha
      android:fromAlpha="0"
      android:toAlpha="1"
      android:interpolator="@android:anim/accelerate_decelerate_interpolator"
      />
    
    

    layout_animation_from_bottom.xml

     <?xml version=1.0 encoding=utf-8?>
     <layoutAnimation
     xmlns:android=http://schemas.android.com/apk/res/android
     android:animation=@anim/item_animation_from_bottom
     android:delay=15%
     android:animationOrder=normal
     />
    

    相关文章

      网友评论

        本文标题:RecycleView 入场动画

        本文链接:https://www.haomeiwen.com/subject/lpjmbqtx.html