Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the duration of native actionbar.show() and hide() animation

I want to do some animation when the actionbar is animating. However, I don't know the duration of actionbar animation.

like image 836
vincentzhou Avatar asked Dec 17 '14 08:12

vincentzhou


1 Answers

Unfortunately, the native actionbar animating time seems to be hard-coded in the Android source code after version 4.1 (with no attributes, no dimensions, no methods) and it's set to 250ms:

Example of the implementaiton of actionbar.hide() method in Android 4.1/4.4:

public void doHide(boolean fromSystem) {
    if (mCurrentShowAnim != null) {
        mCurrentShowAnim.end();
    }

    if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
            || fromSystem)) {
        mContainerView.setAlpha(1);
        mContainerView.setTransitioning(true);
        AnimatorSet anim = new AnimatorSet();
        float endingY = -mContainerView.getHeight();
        if (fromSystem) {
            int topLeft[] = {0, 0};
            mContainerView.getLocationInWindow(topLeft);
            endingY -= topLeft[1];
        }
        ObjectAnimator a = ObjectAnimator.ofFloat(mContainerView, View.TRANSLATION_Y, endingY);
        a.addUpdateListener(mUpdateListener);
        AnimatorSet.Builder b = anim.play(a);
        if (mContentAnimations && mContentView != null) {
            b.with(ObjectAnimator.ofFloat(mContentView, View.TRANSLATION_Y,
                    0, endingY));
        }
        if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
            mSplitView.setAlpha(1);
            b.with(ObjectAnimator.ofFloat(mSplitView, View.TRANSLATION_Y,
                    mSplitView.getHeight()));
        }
        anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
                com.android.internal.R.interpolator.accelerate_cubic));
        anim.setDuration(250);
        anim.addListener(mHideListener);
        mCurrentShowAnim = anim;
        anim.start();
    } else {
        mHideListener.onAnimationEnd(null);

This happens also on Lollipop:

public void doHide(boolean fromSystem) {
        if (mCurrentShowAnim != null) {
            mCurrentShowAnim.end();
        }
        if (mCurWindowVisibility == View.VISIBLE && (mShowHideAnimationEnabled
                || fromSystem)) {
            mContainerView.setAlpha(1);
            mContainerView.setTransitioning(true);
            AnimatorSet anim = new AnimatorSet();
            float endingY = -mContainerView.getHeight();
            if (fromSystem) {
                int topLeft[] = {0, 0};
                mContainerView.getLocationInWindow(topLeft);
                endingY -= topLeft[1];
            }
            ObjectAnimator a = ObjectAnimator.ofFloat(mContainerView, View.TRANSLATION_Y, endingY);
            a.addUpdateListener(mUpdateListener);
            AnimatorSet.Builder b = anim.play(a);
            if (mContentAnimations && mContentView != null) {
                b.with(ObjectAnimator.ofFloat(mContentView, View.TRANSLATION_Y,
                        0, endingY));
            }
            if (mSplitView != null && mSplitView.getVisibility() == View.VISIBLE) {
                mSplitView.setAlpha(1);
                b.with(ObjectAnimator.ofFloat(mSplitView, View.TRANSLATION_Y,
                        mSplitView.getHeight()));
            }
            anim.setInterpolator(AnimationUtils.loadInterpolator(mContext,
                    com.android.internal.R.interpolator.accelerate_cubic));
            anim.setDuration(250);
            anim.addListener(mHideListener);
            mCurrentShowAnim = anim;
            anim.start();
        } else {
            mHideListener.onAnimationEnd(null);
        }
    }

Before 4.1 (e.g. 4.0.1) there's no hardcoded duration value (actually there's no value at all), anyway you could use reflection to access the Animator's duration after the first animation. The field to access is the following:

private Animator mCurrentShowAnim;

I know that this is not a complete answer, but I think it might be helpful anyway.

like image 126
bonnyz Avatar answered May 24 '23 10:05

bonnyz