AppBarLayout.setExpanded(boolean, true) 支持库 23.1.1 中的奇怪动画

AppBarLayout.setExpanded(boolean, true) weird animation in support library 23.1.1

在我的应用程序中,我使用 setExpanded(boolean, true) 在特定事件上扩展或收缩 AppBarLayout

我得到了一个很好的结果,使用 com.android.support:design:23.1.0 制作了一个活泼流畅的动画,然后我更新到 23.1.1 并且动画变得非常慢而且一点也不活泼。

android.support.design.widget.AppBarLayout的源代码中,我定位问题在animateOffsetTo(在public static class Behavior extends HeaderBehavior<AppBarLayout>下),在23.1.0版本中是这样的:

private void animateOffsetTo(final CoordinatorLayout coordinatorLayout,
    final AppBarLayout child, int offset) {
   if (mAnimator == null) {
       mAnimator = ViewUtils.createAnimator();
       mAnimator.setInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);
       mAnimator.setUpdateListener(new ValueAnimatorCompat.AnimatorUpdateListener() {

           @Override
           public void onAnimationUpdate(ValueAnimatorCompat animator) {
               setHeaderTopBottomOffset(coordinatorLayout, child,
                    animator.getAnimatedIntValue());
           }
       });
   } else {
       mAnimator.cancel();
   }
   mAnimator.setIntValues(getTopBottomOffsetForScrollingSibling(), offset);
   mAnimator.start();
}

而在23.1.1版本中是这样的:

private void animateOffsetTo(final CoordinatorLayout coordinatorLayout,
    final AppBarLayout child, final int offset) {
   final int currentOffset = getTopBottomOffsetForScrollingSibling();
   if (currentOffset == offset) {
       if (mAnimator != null && mAnimator.isRunning()) {
           mAnimator.cancel();
       }
       return;
   }
   if (mAnimator == null) {
       mAnimator = ViewUtils.createAnimator();
       mAnimator.setInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);
       mAnimator.setUpdateListener(new ValueAnimatorCompat.AnimatorUpdateListener() {
           @Override
           public void onAnimationUpdate(ValueAnimatorCompat animator) {
            setHeaderTopBottomOffset(coordinatorLayout, child,
                    animator.getAnimatedIntValue());
           }
       });
   } else {
       mAnimator.cancel();
   }
   // Set the duration based on the amount of dips we're travelling in
   final float distanceDp = Math.abs(currentOffset - offset) /
        coordinatorLayout.getResources().getDisplayMetrics().density;
   mAnimator.setDuration(Math.round(distanceDp * 1000 / ANIMATE_OFFSET_DIPS_PER_SECOND));
   mAnimator.setIntValues(currentOffset, offset);
   mAnimator.start();
}

如何更改 expand/contract 动画并使制作速度更快?

The issue was reported and fixed

只需将库更新到 25.3.1 版本。