安卓

来源:互联网 发布:八级钳工 知乎 编辑:程序博客网 时间:2024/06/09 22:42

RecyclerView Animators 的 Github 地址 
RecyclerView的动画集合框架,为你的RecyclerView带来各种各样的动画效果,丰富用户体验。
动画能区分为:

  • ItemAnimator 子控件动画:添加和移除子控件的时候显示的动画

    ItemAnimator
  • AdaptersAnimator 适配器动画:滑动控件时,子控件的显示动画

    AdaptersAnimator
接入:

在gradle中配置

dependencies {  compile 'jp.wasabeef:recyclerview-animators:2.2.6'}

官方示例解析:

ItemAnimator

按效果的分类,列举框架提供的 ItemAnimator :

类型动画实现CoolLandingAnimatorScaleScaleInAnimator,ScaleInTopAnimator,ScaleInBottomAnimator,ScaleInLeftAnimator,ScaleInRightAnimatorFadeFadeInAnimator,FadeInDownAnimator,FadeInUpAnimator,FadeInLeftAnimator,FadeInRightAnimatorFlipFlipInTopXAnimator,FlipInBottomXAnimator,FlipInLeftYAnimator,FlipInRightYAnimatorSlideSlideInLeftAnimator,SlideInRightAnimator,OvershootInLeftAnimator,OvershootInRightAnimator,SlideInUpAnimator,SlideInDownAnimator

使用方法:

// Step1RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);recyclerView.setItemAnimator(new SlideInLeftAnimator());// orSlideInUpAnimator animator = new SlideInUpAnimator(new OvershootInterpolator(1f));recyclerView.setItemAnimator(animator);
  • recyclerView.setItemAnimator(new SlideInLeftAnimator())
    在这一步为recyclerView设置了一个SlideInLeftAnimator的Item动画,
    而SlideInLeftAnimator是框架提供的一个左进左出的动画。

  • new SlideInLeftAnimator(new OvershootInterpolator(1f))
    SlideInLeftAnimator构造时可以为其添加插值器Interpolator,
    OvershootInterpolator是一个越界后返回的插值器,参数为越界程度 (越大越飘逸)
    关于插值器,可以参考 Android学习--Interpolator(插值器)

// Step2/* * Please use the following * notifyItemChanged(int) * notifyItemInserted(int) * notifyItemRemoved(int) * notifyItemRangeChanged(int, int) * notifyItemRangeInserted(int, int) * notifyItemRangeRemoved(int, int) *//* * If you want your animations to work,  * do not rely on calling notifyDataSetChanged(), * as it is the RecyclerView’s default behavior,  * animations are not triggered to start inside this method. */public void remove(int position) {  mDataSet.remove(position);  notifyItemRemoved(position);}public void add(String text, int position) {  mDataSet.add(position, text);  notifyItemInserted(position);}
  • 如果调用notifyDataSetChanged通知更新,是不会有动画效果的,需要使用列举的方法才会有动画效果。
  • 举例了notifyItemRemoved移除通知和notifyItemInserted插入通知,这方面可以另外了解各个方法的不同。
// Advanced Step 3// You can change the durations.recyclerView.getItemAnimator().setAddDuration(1000);recyclerView.getItemAnimator().setRemoveDuration(1000);recyclerView.getItemAnimator().setMoveDuration(1000);recyclerView.getItemAnimator().setChangeDuration(1000);
  • 嗯,通过调用以上的方法,对各种 Item 动画的时长进行设置
// Advanced Step 4// Change the interpolator.SlideInLeftAnimator animator = new SlideInLeftAnimator();animator.setInterpolator(new OvershootInterpolator()); // 补充说明,这东西默认值是2f/* or  * recyclerView.setItemAnimator( *         new SlideInUpAnimator(new OvershootInterpolator(1f)); */recyclerView.setItemAnimator(animator);
  • 就是说除了构造函数外,也可以通过调用setInterpolator设置插值器
// Advanced Step 5// By implementing AnimateViewHolder, you can override preset animation. // So, custom animation can be set depending on view holder.static class MyViewHolder         extends RecyclerView.ViewHolder         implements AnimateViewHolder { ... }
  • 你可以通过让你的ViewHolder实现 AnimateViewHolder 接口,重写动画实现。
    框架会优先使用 AnimateViewHolder 的动画,其实才是 ItemAnimator 定义的动画,
    通过这种实现方式,你可以根据不同的View,实现不同的Item动画。
  • 由于对AnimateViewHolder接口的处理实现是由框架的 BaseItemAnimator 提供的,
    所以要使该功能生效,必须为RecyclerView设置一个继承于BaseItemAnimator 的 ItemAnimator ,
    或者自行对 AnimateViewHolder 进行处理 (什么用框架呢手动滑稽)

AnimationAdapter

按效果的分类,列举框架提供的 AnimationAdapter :

类型动画实现AlphaAlphaInAnimationAdapterScaleScaleInAnimationAdapterSlideSlideInBottomAnimationAdapter,SlideInRightAnimationAdapter,SlideInLeftAnimationAdapter

使用方法:

// Step 1// Set RecyclerView ItemAnimator.RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);MyAdapter adapter = new MyAdapter();recyclerView.setAdapter(new AlphaInAnimationAdapter(adapter));
  • recyclerView.setAdapter(new AlphaInAnimationAdapter(adapter))
    在setAdapter的时候,对原来的Adapter使用 AlphaInAnimationAdapter 进行包装,实现了滑动渐显效果。
  • 通过使用不同的AnimationAdapter,可以实现不同的滑动动画效果。
// Advanced Step 2// Change the durations.MyAdapter adapter = new MyAdapter();AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);alphaAdapter.setDuration(1000);recyclerView.setAdapter(alphaAdapter);
  • 通过调用包装类的setDuration方法,可以设置动画的时长。
// Advanced Step 3// Change the interpolator.MyAdapter adapter = new MyAdapter();AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);alphaAdapter.setInterpolator(new OvershootInterpolator());recyclerView.setAdapter(alphaAdapter);
  • 通过包装类的setInterpolator方法,可以设置插值器。嗯,和上面的Item动画一样的。
// Advanced Step 4// Disable the first scroll mode.MyAdapter adapter = new MyAdapter();AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);scaleAdapter.setFirstOnly(false);recyclerView.setAdapter(alphaAdapter);
  • 一个关键点,通过包装类的setFirstOnly方法,可以对显示方式进行控制。
    在默认的情况下,滑动动画只会在Item第一次显示的时候执行,
    通过设置isFirstOnly为false,可以取消仅第一次显示,实现常驻的滑动动画效果。
// Advanced Step 5// Multiple AnimationsMyAdapter adapter = new MyAdapter();AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);recyclerView.setAdapter(new ScaleInAnimationAdapter(alphaAdapter));
  • 可以通过对包装类的再次包装,实现多重动画效果,如示例中的动画效果会变为 Alpha + Scale。
    AnimationAdapter这下牛逼了,这个包装类可以被包装类再次包装,攻击力增加200%

以上就是RecyclerView Animators的使用方式。

0 0
原创粉丝点击