美文网首页recycleview
RecyclerView<第二十一篇>:条目动画之缩放动画(属性

RecyclerView<第二十一篇>:条目动画之缩放动画(属性

作者: NoBugException | 来源:发表于2019-10-23 17:27 被阅读0次

【整体刷新效果】

当第一次显示RecyclerView的Item或者往下滑动时的动画效果如下:

217.gif

【刷新指定Item】

218.gif

刷新指定Item代码:

            urls.set(position, "http://img4.imgtn.bdimg.com/it/u=3209370120,2008812818&fm=26&gp=0.jpg");
            recycleViewAdapter.notifyItemChanged(position);

【删除效果】

219.gif

删除刷新代码:

            urls.remove(position);
            recycleViewAdapter.notifyItemRemoved(position);
            recycleViewAdapter.notifyItemRangeChanged(position, urls.size() - position);

【插入效果】

220.gif

插入刷新代码:

            urls.add(position, "http://img4.imgtn.bdimg.com/it/u=3209370120,2008812818&fm=26&gp=0.jpg");
            recycleViewAdapter.notifyItemInserted(position);
            recycleViewAdapter.notifyItemRangeChanged(position, urls.size() - position);

【移动效果】

221.gif

移动刷新代码:

            int fromPosition = position;
            int toPosition = position + 1;
            Collections.swap(urls,fromPosition,toPosition);
            recycleViewAdapter.notifyItemMoved(fromPosition, toPosition);
            recycleViewAdapter.notifyItemRangeChanged(position, toPosition - fromPosition + 1);
缩放动画实现代码如下:

AnimationAdapter.java

public abstract class AnimationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  private RecyclerView.Adapter<RecyclerView.ViewHolder> mAdapter;
  private int mDuration = 300;
  private Interpolator mInterpolator = new LinearInterpolator();
  private int mLastPosition = -1;

  private boolean isFirstOnly = true;

  public AnimationAdapter(RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {
    mAdapter = adapter;
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return mAdapter.onCreateViewHolder(parent, viewType);
  }

  @Override
  public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    super.registerAdapterDataObserver(observer);
    mAdapter.registerAdapterDataObserver(observer);
  }

  @Override
  public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    super.unregisterAdapterDataObserver(observer);
    mAdapter.unregisterAdapterDataObserver(observer);
  }

  @Override
  public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    mAdapter.onAttachedToRecyclerView(recyclerView);
  }

  @Override
  public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
    super.onDetachedFromRecyclerView(recyclerView);
    mAdapter.onDetachedFromRecyclerView(recyclerView);
  }

  @Override
  public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
    super.onViewAttachedToWindow(holder);
    mAdapter.onViewAttachedToWindow(holder);
  }

  @Override
  public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
    super.onViewDetachedFromWindow(holder);
    mAdapter.onViewDetachedFromWindow(holder);
  }

  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    mAdapter.onBindViewHolder(holder, position);

    int adapterPosition = holder.getAdapterPosition();
    if (!isFirstOnly || adapterPosition > mLastPosition) {
      for (Animator anim : getAnimators(holder.itemView)) {
        anim.setDuration(mDuration).start();
        anim.setInterpolator(mInterpolator);
      }
      mLastPosition = adapterPosition;
    } else {
      holder.itemView.setScaleX(1);
      holder.itemView.setScaleY(1);
    }
  }

  @Override public void onViewRecycled(RecyclerView.ViewHolder holder) {
    mAdapter.onViewRecycled(holder);
    super.onViewRecycled(holder);
  }

  @Override
  public int getItemCount() {
    return mAdapter.getItemCount();
  }

  public void setDuration(int duration) {
    mDuration = duration;
  }

  public void setInterpolator(Interpolator interpolator) {
    mInterpolator = interpolator;
  }

  public void setStartPosition(int start) {
    mLastPosition = start;
  }

  protected abstract Animator[] getAnimators(View view);

  public void setFirstOnly(boolean firstOnly) {
    isFirstOnly = firstOnly;
  }

  @Override
  public int getItemViewType(int position) {
    return mAdapter.getItemViewType(position);
  }

  public RecyclerView.Adapter<RecyclerView.ViewHolder> getWrappedAdapter() {
    return mAdapter;
  }

  @Override
  public long getItemId(int position) {
    return mAdapter.getItemId(position);
  }
}

ScaleInAnimationAdapter.java

public class ScaleInAnimationAdapter extends AnimationAdapter {

  private static final float DEFAULT_SCALE_FROM = .5f;
  private final float mFrom;

  public ScaleInAnimationAdapter(RecyclerView.Adapter adapter) {
    this(adapter, DEFAULT_SCALE_FROM);
  }

  public ScaleInAnimationAdapter(RecyclerView.Adapter adapter, float from) {
    super(adapter);
    mFrom = from;
  }

  @Override protected Animator[] getAnimators(View view) {
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", mFrom, 1f);
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", mFrom, 1f);
    return new ObjectAnimator[] { scaleX, scaleY };
  }
}

使用缩放Adapter

    ScaleInAnimationAdapter adapter = new ScaleInAnimationAdapter(recycleViewAdapter);
    adapter.setDuration(3000);
    adapter.setFirstOnly(false);
    recyclerview.setAdapter(adapter);

[本章完...]

相关文章

  • RecyclerView<第二十一篇>:条目动画之缩放动画(属性

    【整体刷新效果】 当第一次显示RecyclerView的Item或者往下滑动时的动画效果如下: 【刷新指定Item...

  • RecyclerView<第十六篇>:条目动画之自定义Defau

    为了让RecyclerView更加用户友好,可以给条目添加动画,RecyclerView可以设置条目动画 设置条目...

  • RecyclerView之条目动画

    一,自带的条目动画 我们先来看看系统自带的动画设置实现: 这样我们就实现了动画的添加,通过添加和删除可以看出默认的...

  • Android动画原理

    动画分类 补间动画 旋转、位移、透明度、缩放 属性动画 同样的属性动画也可以做到对View进行缩放、移动、旋转以及...

  • 动画深入研究

    前言 分类 View动画,帧动画,自定义View动画,属性动画 View动画 平移,缩放,旋转,透明Transla...

  • Android动画--View动画(补间动画)

    类型 平移动画 translate 缩放动画 scale 旋转动画 rotate 透明度动画 alpha 共同属性...

  • Android中的动画概述

    动画可以分为三类:View动画,帧动画,属性动画。 一、View动画 1.View动画包括四种:平移动画,缩放动画...

  • Android动画分类

    动画分类 View动画、帧动画、属性动画 View动画包括:平移、旋转、缩放、透明度,View动画是一种渐近式动画...

  • android动画

    动画: 分类:分为视图动画和属性动画,其中视图动画又分为补间动画和逐帧动画。补间动画又分为平移动画、缩放动画、旋转...

  • Android动画

    Android动画的分类:三种,属性动画,补间动画,帧动画 补间动画:缩放,平移,旋转,透明度 API:Anima...

网友评论

    本文标题:RecyclerView<第二十一篇>:条目动画之缩放动画(属性

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