美文网首页Android应用开发那些事
RecyclerView<第二十七篇>:条目动画之ScaleIn

RecyclerView<第二十七篇>:条目动画之ScaleIn

作者: NoBugException | 来源:发表于2019-10-23 23:18 被阅读0次
    (1)效果图

    【更新数据】

    230.gif

    【插入数据】

    239.gif

    【删除数据】

    240.gif

    【移动数据】

    236.gif
    (2)ScaleInBottomAnimator类代码

    AnimateViewHolder.java

    public interface AnimateViewHolder {
    
      void preAnimateAddImpl(final RecyclerView.ViewHolder holder);
    
      void preAnimateRemoveImpl(final RecyclerView.ViewHolder holder);
    
      void animateAddImpl(final RecyclerView.ViewHolder holder, ViewPropertyAnimatorListener listener);
    
      void animateRemoveImpl(final RecyclerView.ViewHolder holder,
                             ViewPropertyAnimatorListener listener);
    }
    

    BaseItemAnimator.java

    public abstract class BaseItemAnimator extends SimpleItemAnimator {
    
      private static final boolean DEBUG = false;
    
      private ArrayList<ViewHolder> mPendingRemovals = new ArrayList<>();
      private ArrayList<ViewHolder> mPendingAdditions = new ArrayList<>();
      private ArrayList<MoveInfo> mPendingMoves = new ArrayList<>();
      private ArrayList<ChangeInfo> mPendingChanges = new ArrayList<>();
    
      private ArrayList<ArrayList<ViewHolder>> mAdditionsList = new ArrayList<>();
      private ArrayList<ArrayList<MoveInfo>> mMovesList = new ArrayList<>();
      private ArrayList<ArrayList<ChangeInfo>> mChangesList = new ArrayList<>();
    
      protected ArrayList<ViewHolder> mAddAnimations = new ArrayList<>();
      private ArrayList<ViewHolder> mMoveAnimations = new ArrayList<>();
      protected ArrayList<ViewHolder> mRemoveAnimations = new ArrayList<>();
      private ArrayList<ViewHolder> mChangeAnimations = new ArrayList<>();
    
      protected Interpolator mInterpolator = new DecelerateInterpolator();
    
      private static class MoveInfo {
    
        public ViewHolder holder;
        public int fromX, fromY, toX, toY;
    
        private MoveInfo(ViewHolder holder, int fromX, int fromY, int toX, int toY) {
          this.holder = holder;
          this.fromX = fromX;
          this.fromY = fromY;
          this.toX = toX;
          this.toY = toY;
        }
      }
    
      private static class ChangeInfo {
    
        public ViewHolder oldHolder, newHolder;
        public int fromX, fromY, toX, toY;
    
        private ChangeInfo(ViewHolder oldHolder, ViewHolder newHolder) {
          this.oldHolder = oldHolder;
          this.newHolder = newHolder;
        }
    
        private ChangeInfo(ViewHolder oldHolder, ViewHolder newHolder, int fromX, int fromY, int toX,
            int toY) {
          this(oldHolder, newHolder);
          this.fromX = fromX;
          this.fromY = fromY;
          this.toX = toX;
          this.toY = toY;
        }
    
        @Override public String toString() {
          return "ChangeInfo{" +
              "oldHolder=" + oldHolder +
              ", newHolder=" + newHolder +
              ", fromX=" + fromX +
              ", fromY=" + fromY +
              ", toX=" + toX +
              ", toY=" + toY +
              '}';
        }
      }
    
      public BaseItemAnimator() {
        super();
        setSupportsChangeAnimations(false);
      }
    
      public void setInterpolator(Interpolator mInterpolator) {
        this.mInterpolator = mInterpolator;
      }
    
      @Override public void runPendingAnimations() {
        boolean removalsPending = !mPendingRemovals.isEmpty();
        boolean movesPending = !mPendingMoves.isEmpty();
        boolean changesPending = !mPendingChanges.isEmpty();
        boolean additionsPending = !mPendingAdditions.isEmpty();
        if (!removalsPending && !movesPending && !additionsPending && !changesPending) {
          // nothing to animate
          return;
        }
        // First, remove stuff
        for (ViewHolder holder : mPendingRemovals) {
          doAnimateRemove(holder);
        }
        mPendingRemovals.clear();
        // Next, move stuff
        if (movesPending) {
          final ArrayList<MoveInfo> moves = new ArrayList<MoveInfo>();
          moves.addAll(mPendingMoves);
          mMovesList.add(moves);
          mPendingMoves.clear();
          Runnable mover = new Runnable() {
            @Override public void run() {
              boolean removed = mMovesList.remove(moves);
              if (!removed) {
                // already canceled
                return;
              }
              for (MoveInfo moveInfo : moves) {
                animateMoveImpl(moveInfo.holder, moveInfo.fromX, moveInfo.fromY, moveInfo.toX,
                    moveInfo.toY);
              }
              moves.clear();
            }
          };
          if (removalsPending) {
            View view = moves.get(0).holder.itemView;
            ViewCompat.postOnAnimationDelayed(view, mover, getRemoveDuration());
          } else {
            mover.run();
          }
        }
        // Next, change stuff, to run in parallel with move animations
        if (changesPending) {
          final ArrayList<ChangeInfo> changes = new ArrayList<ChangeInfo>();
          changes.addAll(mPendingChanges);
          mChangesList.add(changes);
          mPendingChanges.clear();
          Runnable changer = new Runnable() {
            @Override public void run() {
              boolean removed = mChangesList.remove(changes);
              if (!removed) {
                // already canceled
                return;
              }
              for (ChangeInfo change : changes) {
                animateChangeImpl(change);
              }
              changes.clear();
            }
          };
          if (removalsPending) {
            ViewHolder holder = changes.get(0).oldHolder;
            ViewCompat.postOnAnimationDelayed(holder.itemView, changer, getRemoveDuration());
          } else {
            changer.run();
          }
        }
        // Next, add stuff
        if (additionsPending) {
          final ArrayList<ViewHolder> additions = new ArrayList<ViewHolder>();
          additions.addAll(mPendingAdditions);
          mAdditionsList.add(additions);
          mPendingAdditions.clear();
          Runnable adder = new Runnable() {
            public void run() {
              boolean removed = mAdditionsList.remove(additions);
              if (!removed) {
                // already canceled
                return;
              }
              for (ViewHolder holder : additions) {
                doAnimateAdd(holder);
              }
              additions.clear();
            }
          };
          if (removalsPending || movesPending || changesPending) {
            long removeDuration = removalsPending ? getRemoveDuration() : 0;
            long moveDuration = movesPending ? getMoveDuration() : 0;
            long changeDuration = changesPending ? getChangeDuration() : 0;
            long totalDelay = removeDuration + Math.max(moveDuration, changeDuration);
            View view = additions.get(0).itemView;
            ViewCompat.postOnAnimationDelayed(view, adder, totalDelay);
          } else {
            adder.run();
          }
        }
      }
    
      protected void preAnimateRemoveImpl(final ViewHolder holder) {
      }
    
      protected void preAnimateAddImpl(final ViewHolder holder) {
      }
    
      protected abstract void animateRemoveImpl(final ViewHolder holder);
    
      protected abstract void animateAddImpl(final ViewHolder holder);
    
      private void preAnimateRemove(final ViewHolder holder) {
        ViewHelper.clear(holder.itemView);
    
        if (holder instanceof AnimateViewHolder) {
          ((AnimateViewHolder) holder).preAnimateRemoveImpl(holder);
        } else {
          preAnimateRemoveImpl(holder);
        }
      }
    
      private void preAnimateAdd(final ViewHolder holder) {
        ViewHelper.clear(holder.itemView);
    
        if (holder instanceof AnimateViewHolder) {
          ((AnimateViewHolder) holder).preAnimateAddImpl(holder);
        } else {
          preAnimateAddImpl(holder);
        }
      }
    
      private void doAnimateRemove(final ViewHolder holder) {
        if (holder instanceof AnimateViewHolder) {
          ((AnimateViewHolder) holder).animateRemoveImpl(holder, new DefaultRemoveVpaListener(holder));
        } else {
          animateRemoveImpl(holder);
        }
    
        mRemoveAnimations.add(holder);
      }
    
      private void doAnimateAdd(final ViewHolder holder) {
        if (holder instanceof AnimateViewHolder) {
          ((AnimateViewHolder) holder).animateAddImpl(holder, new DefaultAddVpaListener(holder));
        } else {
          animateAddImpl(holder);
        }
    
        mAddAnimations.add(holder);
      }
    
      @Override public boolean animateRemove(final ViewHolder holder) {
        endAnimation(holder);
        preAnimateRemove(holder);
        mPendingRemovals.add(holder);
        return true;
      }
    
      protected long getRemoveDelay(final ViewHolder holder) {
        return Math.abs(holder.getOldPosition() * getRemoveDuration() / 4);
      }
    
      @Override public boolean animateAdd(final ViewHolder holder) {
        endAnimation(holder);
        preAnimateAdd(holder);
        mPendingAdditions.add(holder);
        return true;
      }
    
      protected long getAddDelay(final ViewHolder holder) {
        return Math.abs(holder.getAdapterPosition() * getAddDuration() / 4);
      }
    
      @Override
      public boolean animateMove(final ViewHolder holder, int fromX, int fromY, int toX, int toY) {
        final View view = holder.itemView;
        fromX += ViewCompat.getTranslationX(holder.itemView);
        fromY += ViewCompat.getTranslationY(holder.itemView);
        endAnimation(holder);
        int deltaX = toX - fromX;
        int deltaY = toY - fromY;
        if (deltaX == 0 && deltaY == 0) {
          dispatchMoveFinished(holder);
          return false;
        }
        if (deltaX != 0) {
          ViewCompat.setTranslationX(view, -deltaX);
        }
        if (deltaY != 0) {
          ViewCompat.setTranslationY(view, -deltaY);
        }
        mPendingMoves.add(new MoveInfo(holder, fromX, fromY, toX, toY));
        return true;
      }
    
      private void animateMoveImpl(final ViewHolder holder, int fromX, int fromY, int toX, int toY) {
        final View view = holder.itemView;
        final int deltaX = toX - fromX;
        final int deltaY = toY - fromY;
        if (deltaX != 0) {
          ViewCompat.animate(view).translationX(0);
        }
        if (deltaY != 0) {
          ViewCompat.animate(view).translationY(0);
        }
        // TODO: make EndActions end listeners instead, since end actions aren't called when
        // vpas are canceled (and can't end them. why?)
        // need listener functionality in VPACompat for this. Ick.
        mMoveAnimations.add(holder);
        final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view);
        animation.setDuration(getMoveDuration()).setListener(new VpaListenerAdapter() {
          @Override public void onAnimationStart(View view) {
            dispatchMoveStarting(holder);
          }
    
          @Override public void onAnimationCancel(View view) {
            if (deltaX != 0) {
              ViewCompat.setTranslationX(view, 0);
            }
            if (deltaY != 0) {
              ViewCompat.setTranslationY(view, 0);
            }
          }
    
          @Override public void onAnimationEnd(View view) {
            animation.setListener(null);
            dispatchMoveFinished(holder);
            mMoveAnimations.remove(holder);
            dispatchFinishedWhenDone();
          }
        }).start();
      }
    
      @Override
      public boolean animateChange(ViewHolder oldHolder, ViewHolder newHolder, int fromX, int fromY, int toX, int toY) {
        if (oldHolder == newHolder) {
          // Don't know how to run change animations when the same view holder is re-used.
          // run a move animation to handle position changes.
          return animateMove(oldHolder, fromX, fromY, toX, toY);
        }
        final float prevTranslationX = ViewCompat.getTranslationX(oldHolder.itemView);
        final float prevTranslationY = ViewCompat.getTranslationY(oldHolder.itemView);
        final float prevAlpha = ViewCompat.getAlpha(oldHolder.itemView);
        endAnimation(oldHolder);
        int deltaX = (int) (toX - fromX - prevTranslationX);
        int deltaY = (int) (toY - fromY - prevTranslationY);
        // recover prev translation state after ending animation
        ViewCompat.setTranslationX(oldHolder.itemView, prevTranslationX);
        ViewCompat.setTranslationY(oldHolder.itemView, prevTranslationY);
        ViewCompat.setAlpha(oldHolder.itemView, prevAlpha);
        if (newHolder != null && newHolder.itemView != null) {
          // carry over translation values
          endAnimation(newHolder);
          ViewCompat.setTranslationX(newHolder.itemView, -deltaX);
          ViewCompat.setTranslationY(newHolder.itemView, -deltaY);
          ViewCompat.setAlpha(newHolder.itemView, 0);
        }
        mPendingChanges.add(new ChangeInfo(oldHolder, newHolder, fromX, fromY, toX, toY));
        return true;
      }
    
      private void animateChangeImpl(final ChangeInfo changeInfo) {
        final ViewHolder holder = changeInfo.oldHolder;
        final View view = holder == null ? null : holder.itemView;
        final ViewHolder newHolder = changeInfo.newHolder;
        final View newView = newHolder != null ? newHolder.itemView : null;
        if (view != null) {
          mChangeAnimations.add(changeInfo.oldHolder);
          final ViewPropertyAnimatorCompat oldViewAnim =
              ViewCompat.animate(view).setDuration(getChangeDuration());
          oldViewAnim.translationX(changeInfo.toX - changeInfo.fromX);
          oldViewAnim.translationY(changeInfo.toY - changeInfo.fromY);
          oldViewAnim.alpha(0).setListener(new VpaListenerAdapter() {
            @Override public void onAnimationStart(View view) {
              dispatchChangeStarting(changeInfo.oldHolder, true);
            }
    
            @Override public void onAnimationEnd(View view) {
              oldViewAnim.setListener(null);
              ViewCompat.setAlpha(view, 1);
              ViewCompat.setTranslationX(view, 0);
              ViewCompat.setTranslationY(view, 0);
              dispatchChangeFinished(changeInfo.oldHolder, true);
              mChangeAnimations.remove(changeInfo.oldHolder);
              dispatchFinishedWhenDone();
            }
          }).start();
        }
        if (newView != null) {
          mChangeAnimations.add(changeInfo.newHolder);
          final ViewPropertyAnimatorCompat newViewAnimation = ViewCompat.animate(newView);
          newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()).
              alpha(1).setListener(new VpaListenerAdapter() {
            @Override public void onAnimationStart(View view) {
              dispatchChangeStarting(changeInfo.newHolder, false);
            }
    
            @Override public void onAnimationEnd(View view) {
              newViewAnimation.setListener(null);
              ViewCompat.setAlpha(newView, 1);
              ViewCompat.setTranslationX(newView, 0);
              ViewCompat.setTranslationY(newView, 0);
              dispatchChangeFinished(changeInfo.newHolder, false);
              mChangeAnimations.remove(changeInfo.newHolder);
              dispatchFinishedWhenDone();
            }
          }).start();
        }
      }
    
      private void endChangeAnimation(List<ChangeInfo> infoList, ViewHolder item) {
        for (int i = infoList.size() - 1; i >= 0; i--) {
          ChangeInfo changeInfo = infoList.get(i);
          if (endChangeAnimationIfNecessary(changeInfo, item)) {
            if (changeInfo.oldHolder == null && changeInfo.newHolder == null) {
              infoList.remove(changeInfo);
            }
          }
        }
      }
    
      private void endChangeAnimationIfNecessary(ChangeInfo changeInfo) {
        if (changeInfo.oldHolder != null) {
          endChangeAnimationIfNecessary(changeInfo, changeInfo.oldHolder);
        }
        if (changeInfo.newHolder != null) {
          endChangeAnimationIfNecessary(changeInfo, changeInfo.newHolder);
        }
      }
    
      private boolean endChangeAnimationIfNecessary(ChangeInfo changeInfo, ViewHolder item) {
        boolean oldItem = false;
        if (changeInfo.newHolder == item) {
          changeInfo.newHolder = null;
        } else if (changeInfo.oldHolder == item) {
          changeInfo.oldHolder = null;
          oldItem = true;
        } else {
          return false;
        }
        ViewCompat.setAlpha(item.itemView, 1);
        ViewCompat.setTranslationX(item.itemView, 0);
        ViewCompat.setTranslationY(item.itemView, 0);
        dispatchChangeFinished(item, oldItem);
        return true;
      }
    
      @Override public void endAnimation(ViewHolder item) {
        final View view = item.itemView;
        // this will trigger end callback which should set properties to their target values.
        ViewCompat.animate(view).cancel();
        // TODO if some other animations are chained to end, how do we cancel them as well?
        for (int i = mPendingMoves.size() - 1; i >= 0; i--) {
          MoveInfo moveInfo = mPendingMoves.get(i);
          if (moveInfo.holder == item) {
            ViewCompat.setTranslationY(view, 0);
            ViewCompat.setTranslationX(view, 0);
            dispatchMoveFinished(item);
            mPendingMoves.remove(i);
          }
        }
        endChangeAnimation(mPendingChanges, item);
        if (mPendingRemovals.remove(item)) {
          ViewHelper.clear(item.itemView);
          dispatchRemoveFinished(item);
        }
        if (mPendingAdditions.remove(item)) {
          ViewHelper.clear(item.itemView);
          dispatchAddFinished(item);
        }
    
        for (int i = mChangesList.size() - 1; i >= 0; i--) {
          ArrayList<ChangeInfo> changes = mChangesList.get(i);
          endChangeAnimation(changes, item);
          if (changes.isEmpty()) {
            mChangesList.remove(i);
          }
        }
        for (int i = mMovesList.size() - 1; i >= 0; i--) {
          ArrayList<MoveInfo> moves = mMovesList.get(i);
          for (int j = moves.size() - 1; j >= 0; j--) {
            MoveInfo moveInfo = moves.get(j);
            if (moveInfo.holder == item) {
              ViewCompat.setTranslationY(view, 0);
              ViewCompat.setTranslationX(view, 0);
              dispatchMoveFinished(item);
              moves.remove(j);
              if (moves.isEmpty()) {
                mMovesList.remove(i);
              }
              break;
            }
          }
        }
        for (int i = mAdditionsList.size() - 1; i >= 0; i--) {
          ArrayList<ViewHolder> additions = mAdditionsList.get(i);
          if (additions.remove(item)) {
            ViewHelper.clear(item.itemView);
            dispatchAddFinished(item);
            if (additions.isEmpty()) {
              mAdditionsList.remove(i);
            }
          }
        }
    
        // animations should be ended by the cancel above.
        if (mRemoveAnimations.remove(item) && DEBUG) {
          throw new IllegalStateException(
              "after animation is cancelled, item should not be in " + "mRemoveAnimations list");
        }
    
        if (mAddAnimations.remove(item) && DEBUG) {
          throw new IllegalStateException(
              "after animation is cancelled, item should not be in " + "mAddAnimations list");
        }
    
        if (mChangeAnimations.remove(item) && DEBUG) {
          throw new IllegalStateException(
              "after animation is cancelled, item should not be in " + "mChangeAnimations list");
        }
    
        if (mMoveAnimations.remove(item) && DEBUG) {
          throw new IllegalStateException(
              "after animation is cancelled, item should not be in " + "mMoveAnimations list");
        }
        dispatchFinishedWhenDone();
      }
    
      @Override public boolean isRunning() {
        return (!mPendingAdditions.isEmpty() ||
            !mPendingChanges.isEmpty() ||
            !mPendingMoves.isEmpty() ||
            !mPendingRemovals.isEmpty() ||
            !mMoveAnimations.isEmpty() ||
            !mRemoveAnimations.isEmpty() ||
            !mAddAnimations.isEmpty() ||
            !mChangeAnimations.isEmpty() ||
            !mMovesList.isEmpty() ||
            !mAdditionsList.isEmpty() ||
            !mChangesList.isEmpty());
      }
    
      /**
       * Check the state of currently pending and running animations. If there are none
       * pending/running, call #dispatchAnimationsFinished() to notify any
       * listeners.
       */
      private void dispatchFinishedWhenDone() {
        if (!isRunning()) {
          dispatchAnimationsFinished();
        }
      }
    
      @Override public void endAnimations() {
        int count = mPendingMoves.size();
        for (int i = count - 1; i >= 0; i--) {
          MoveInfo item = mPendingMoves.get(i);
          View view = item.holder.itemView;
          ViewCompat.setTranslationY(view, 0);
          ViewCompat.setTranslationX(view, 0);
          dispatchMoveFinished(item.holder);
          mPendingMoves.remove(i);
        }
        count = mPendingRemovals.size();
        for (int i = count - 1; i >= 0; i--) {
          ViewHolder item = mPendingRemovals.get(i);
          dispatchRemoveFinished(item);
          mPendingRemovals.remove(i);
        }
        count = mPendingAdditions.size();
        for (int i = count - 1; i >= 0; i--) {
          ViewHolder item = mPendingAdditions.get(i);
          ViewHelper.clear(item.itemView);
          dispatchAddFinished(item);
          mPendingAdditions.remove(i);
        }
        count = mPendingChanges.size();
        for (int i = count - 1; i >= 0; i--) {
          endChangeAnimationIfNecessary(mPendingChanges.get(i));
        }
        mPendingChanges.clear();
        if (!isRunning()) {
          return;
        }
    
        int listCount = mMovesList.size();
        for (int i = listCount - 1; i >= 0; i--) {
          ArrayList<MoveInfo> moves = mMovesList.get(i);
          count = moves.size();
          for (int j = count - 1; j >= 0; j--) {
            MoveInfo moveInfo = moves.get(j);
            ViewHolder item = moveInfo.holder;
            View view = item.itemView;
            ViewCompat.setTranslationY(view, 0);
            ViewCompat.setTranslationX(view, 0);
            dispatchMoveFinished(moveInfo.holder);
            moves.remove(j);
            if (moves.isEmpty()) {
              mMovesList.remove(moves);
            }
          }
        }
        listCount = mAdditionsList.size();
        for (int i = listCount - 1; i >= 0; i--) {
          ArrayList<ViewHolder> additions = mAdditionsList.get(i);
          count = additions.size();
          for (int j = count - 1; j >= 0; j--) {
            ViewHolder item = additions.get(j);
            View view = item.itemView;
            ViewCompat.setAlpha(view, 1);
            dispatchAddFinished(item);
            //this check prevent exception when removal already happened during finishing animation
            if (j < additions.size()) {
              additions.remove(j);
            }
            if (additions.isEmpty()) {
              mAdditionsList.remove(additions);
            }
          }
        }
        listCount = mChangesList.size();
        for (int i = listCount - 1; i >= 0; i--) {
          ArrayList<ChangeInfo> changes = mChangesList.get(i);
          count = changes.size();
          for (int j = count - 1; j >= 0; j--) {
            endChangeAnimationIfNecessary(changes.get(j));
            if (changes.isEmpty()) {
              mChangesList.remove(changes);
            }
          }
        }
    
        cancelAll(mRemoveAnimations);
        cancelAll(mMoveAnimations);
        cancelAll(mAddAnimations);
        cancelAll(mChangeAnimations);
    
        dispatchAnimationsFinished();
      }
    
      void cancelAll(List<ViewHolder> viewHolders) {
        for (int i = viewHolders.size() - 1; i >= 0; i--) {
          ViewCompat.animate(viewHolders.get(i).itemView).cancel();
        }
      }
    
      private static class VpaListenerAdapter implements ViewPropertyAnimatorListener {
    
        @Override public void onAnimationStart(View view) {
        }
    
        @Override public void onAnimationEnd(View view) {
        }
    
        @Override public void onAnimationCancel(View view) {
        }
      }
    
      protected class DefaultAddVpaListener extends VpaListenerAdapter {
    
        ViewHolder mViewHolder;
    
        public DefaultAddVpaListener(final ViewHolder holder) {
          mViewHolder = holder;
        }
    
        @Override public void onAnimationStart(View view) {
          dispatchAddStarting(mViewHolder);
        }
    
        @Override public void onAnimationCancel(View view) {
          ViewHelper.clear(view);
        }
    
        @Override public void onAnimationEnd(View view) {
          ViewHelper.clear(view);
          dispatchAddFinished(mViewHolder);
          mAddAnimations.remove(mViewHolder);
          dispatchFinishedWhenDone();
        }
      }
    
      protected class DefaultRemoveVpaListener extends VpaListenerAdapter {
    
        ViewHolder mViewHolder;
    
        public DefaultRemoveVpaListener(final ViewHolder holder) {
          mViewHolder = holder;
        }
    
        @Override public void onAnimationStart(View view) {
          dispatchRemoveStarting(mViewHolder);
        }
    
        @Override public void onAnimationCancel(View view) {
          ViewHelper.clear(view);
        }
    
        @Override public void onAnimationEnd(View view) {
          ViewHelper.clear(view);
          dispatchRemoveFinished(mViewHolder);
          mRemoveAnimations.remove(mViewHolder);
          dispatchFinishedWhenDone();
        }
      }
    }
    

    ViewHelper.java

    public final class ViewHelper {
    
      public static void clear(View v) {
        v.setAlpha(1);
        v.setScaleY(1);
        v.setScaleX(1);
        v.setTranslationY(0);
        v.setTranslationX(0);
        v.setRotation(0);
        v.setRotationY(0);
        v.setRotationX(0);
        v.setPivotY(v.getMeasuredHeight() / 2);
        v.setPivotX(v.getMeasuredWidth() / 2);
        ViewCompat.animate(v).setInterpolator(null).setStartDelay(0);
      }
    }
    

    ScaleInBottomAnimator.java

    public class ScaleInBottomAnimator extends BaseItemAnimator {
    
      public ScaleInBottomAnimator() {
      }
    
      public ScaleInBottomAnimator(Interpolator interpolator) {
        mInterpolator = interpolator;
      }
    
      @Override protected void preAnimateRemoveImpl(RecyclerView.ViewHolder holder) {
        // @TODO https://code.google.com/p/android/issues/detail?id=80863
        //        ViewCompat.setPivotY(holder.itemView, holder.itemView.getHeight());
        holder.itemView.setPivotY(holder.itemView.getHeight());
      }
    
      @Override protected void animateRemoveImpl(final RecyclerView.ViewHolder holder) {
        ViewCompat.animate(holder.itemView)
            .scaleX(0)
            .scaleY(0)
            .setDuration(getRemoveDuration())
            .setInterpolator(mInterpolator)
            .setListener(new DefaultRemoveVpaListener(holder))
            .setStartDelay(getRemoveDelay(holder))
            .start();
      }
    
      @Override protected void preAnimateAddImpl(RecyclerView.ViewHolder holder) {
        // @TODO https://code.google.com/p/android/issues/detail?id=80863
        //        ViewCompat.setPivotY(holder.itemView, holder.itemView.getHeight());
        holder.itemView.setPivotY(holder.itemView.getHeight());
        ViewCompat.setScaleX(holder.itemView, 0);
        ViewCompat.setScaleY(holder.itemView, 0);
      }
    
      @Override protected void animateAddImpl(final RecyclerView.ViewHolder holder) {
        ViewCompat.animate(holder.itemView)
            .scaleX(1)
            .scaleY(1)
            .setDuration(getAddDuration())
            .setInterpolator(mInterpolator)
            .setListener(new DefaultAddVpaListener(holder))
            .setStartDelay(getAddDelay(holder))
            .start();
      }
    }
    
    (3)刷新代码

    【更新数据】

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

    【插入数据】

                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);
    

    【删除数据】

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

    【移动数据】

                int fromPosition = position;
                int toPosition = position + 1;
                Collections.swap(urls,fromPosition,toPosition);
                recycleViewAdapter.notifyItemMoved(fromPosition, toPosition);
                recycleViewAdapter.notifyItemRangeChanged(position, toPosition - fromPosition + 1);
    
    (4)如何使用
        ScaleInBottomAnimator scaleInBottomAnimator = new ScaleInBottomAnimator();
        scaleInBottomAnimator.setChangeDuration(300);
        scaleInBottomAnimator.setAddDuration(300);
        scaleInBottomAnimator.setRemoveDuration(300);
        scaleInBottomAnimator.setMoveDuration(3000);
        scaleInBottomAnimator.setInterpolator(new OvershootInterpolator());
        scaleInBottomAnimator.setSupportsChangeAnimations(true);
        recyclerview.setItemAnimator(scaleInBottomAnimator);
        recyclerview.setAdapter(recycleViewAdapter);
    

    [本章完...]

    相关文章

      网友评论

        本文标题:RecyclerView<第二十七篇>:条目动画之ScaleIn

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