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

2

42

In my app I expand or contract the AppBarLayout on a specific event using setExpanded(boolean, true).

I've got a good result, with a snappy and fluid animation using com.android.support:design:23.1.0, then I updated to 23.1.1 and the animation got very slow and not snappy at all.

In the source code of android.support.design.widget.AppBarLayout, I located the problem in animateOffsetTo (under public static class Behavior extends HeaderBehavior<AppBarLayout>), that in the version 23.1.0 was like this:

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

And in the version 23.1.1 is like this:

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

How can I change the expand/contract animation and make is faster?

Danilodanio answered 24/11, 2015 at 11:24 Comment(14)
Change setDuration???...Malapropism
Are you sure this is the exactly problem your are pointing out???Myelencephalon
@MicheleLacorte: How? That's the code of com.android.support:designDanilodanio
@Danilodanio mAnimator.setDuration(value) change value to 100 or what you wantMalapropism
are you running the code on the emulator or phone?Saddler
@lifeevader On a phone (Nexus 4, 5.1.1)Danilodanio
it seems its a Lollipop related bug, am running on 4.2 and its not giving me any issues, how do you plan on animating your AppBar? could u give a sample like of how you want the animation to be like maybe we could find alternativesSaddler
@lifeevader: it is hard to describe, is a "close" animation, like it was on v23.1.0 was perfect. It would be great if it would be possible to set mAnimator.setDuration, but since it is private inside the library is not possible.... :-/Danilodanio
do you mean the expand and contract animation like the one here guides.codepath.com/android/… on gif 2?Saddler
The is right :) But using setExpanded(boolean, true) the animation is automatic, and not dragged by the user.Danilodanio
What about updating appcompat to 23.1.1?Extemporaneous
Any luck on that? I'm facing the same problem.Cussed
@Gilson No, I gave up and I changed my layout to work around that :(Danilodanio
This issue is reported here and is scheduled to be fixed in a future release of the library.Quickstep
W
2

The issue was reported and fixed

Womanly answered 22/2, 2017 at 12:13 Comment(0)
M
1

Just update library to 25.3.1 version.

Misdoing answered 8/5, 2017 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.