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

时间:2015-11-24 11:24:51

标签: android android-support-library android-support-design

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

我得到了一个很好的结果,使用com.android.support:design:23.1.0创作了一个活泼流畅的动画,然后我更新到了23.1.1,动画变得非常缓慢而且没有时髦。

android.support.design.widget.AppBarLayout的源代码中,我将问题放在animateOffsetTopublic 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();
}

如何更改展开/收缩动画并使其更快?

1 个答案:

答案 0 :(得分:2)