Android SupportActionBar 在显示/隐藏时没有动画

标签 android animation android-actionbar

我发现很多关于如何禁用 Android ActionBar 的动画的问题。我有完全相反的问题。我确实想要动画,但它应该开箱即用,对吧?

我认为问题出在我的自定义工具栏上:

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar_parent"
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="@dimen/menu_height"
        android:minHeight="@dimen/menu_height"
        android:background="@color/backgroundColor" app:popupTheme="@style/AppTheme.PopupOverlay"
        android:theme="@style/ToolbarColoredBackArrow"/>

</android.support.design.widget.AppBarLayout>

我在 mij Activity 中这样设置:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

工具栏工作正常,但当我调用以下任一方法时没有动画:

protected void hideActionBar(){
    ActionBar ab = getSupportActionBar();
    if (ab.isShowing()) {
        ab.hide();
    }

}

protected void showActionBar(){
    ActionBar ab = getSupportActionBar();
    if (!ab.isShowing()) {
        ab.show();
    }
}

这是什么原因?

最佳答案

请丢弃我上面的评论。我对此做了一些研究,我上面的建议没有任何意义。

当您调用 setSupportActionBar(Toolbar) 时,会发生以下情况:

public void setSupportActionBar(Toolbar toolbar) {
    ....
    ToolbarActionBar tbab = new ToolbarActionBar(toolbar, ((Activity) mContext).getTitle(),
            mAppCompatWindowCallback);
    mActionBar = tbab;
    ....
}

因此,对 getSupportActionBar() 的后续调用将返回 ToolbarActionBar 的实例。看看这个类是如何实现我们感兴趣的功能的:

setShowHideAnimationEnabled(boolean):如您所述,此方法没有任何区别。

@Override
public void setShowHideAnimationEnabled(boolean enabled) {
    // This space for rent; no-op.
}

show(): 仅使操作栏可见 - 不支持动画。

@Override
public void show() {
    // TODO: Consider a better transition for this.
    // Right now use no automatic transition so that the app can supply one if desired.
    mDecorToolbar.setVisibility(View.VISIBLE);
}

hide(): 仅使操作栏可见 - 同样,不支持动画。

@Override
public void hide() {
    // TODO: Consider a better transition for this.
    // Right now use no automatic transition so that the app can supply one if desired.
    mDecorToolbar.setVisibility(View.GONE);
}

show()hide() 中的注释暗示开发者应该提供动画过渡。也许,像这样:

protected void hideActionBar(){
    final ActionBar ab = getSupportActionBar();
    if (ab != null && ab.isShowing()) {
        if(mToolbar != null) {
            mToolbar.animate().translationY(-112).setDuration(600L)
                    .withEndAction(new Runnable() {
                        @Override
                        public void run() {
                            ab.hide();
                        }
                    }).start();
        } else {
            ab.hide();
        }
    }
}

protected void showActionBar(){
    ActionBar ab = getSupportActionBar();
    if (ab != null && !ab.isShowing()) {
        ab.show();
        if(mToolbar != null) {
            mToolbar.animate().translationY(0).setDuration(600L).start();
        }
    }
}

mToolbar.animate()..... 部分是根据内存编写的 - 语法可能不正确:(。您还可以添加 .alpha(0(during hide)或 1(在演出期间)) 使过渡看起来更好。

实现:

现在应该清楚的是,getSupportActionBar().show() 和 hide() 不关心您使用 Toolbar 以及这些方法做什么电话。此外,Toolbar 将被视为 Activity 中的任何其他 View。牢记这些要点,问题归结为 - 我们如何动画隐藏(以及稍后显示)View。由于我们需要 Activity 内容随着隐藏(或显示)Toolbar 滑动,我建议如下实现。请注意,这只是一个基本的常规例程。你当然可以微调它,或者想出一个完全不同的(阅读更好)动画过渡:

// holds the original Toolbar height.
// this can also be obtained via (an)other method(s)
int mToolbarHeight, mAnimDuration = 600/* milliseconds */;

ValueAnimator mVaActionBar;

void hideActionBar() {
    // initialize `mToolbarHeight`
    if (mToolbarHeight == 0) {
        mToolbarHeight = mToolbar.getHeight();
    }

    if (mVaActionBar != null && mVaActionBar.isRunning()) {
        // we are already animating a transition - block here
        return;
    }

    // animate `Toolbar's` height to zero.
    mVaActionBar = ValueAnimator.ofInt(mToolbarHeight , 0);
    mVaActionBar.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            // update LayoutParams
            ((AppBarLayout.LayoutParams)mToolbar.getLayoutParams()).height
                    = (Integer)animation.getAnimatedValue();
            mToolbar.requestLayout();
        }
    });

    mVaActionBar.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);

            if (getSupportActionBar() != null) { // sanity check
                getSupportActionBar().hide();
            }
        }
    });

    mVaActionBar.setDuration(mAnimDuration);
    mVaActionBar.start();
}

void showActionBar() {
    if (mVaActionBar != null && mVaActionBar.isRunning()) {
        // we are already animating a transition - block here
        return;
    }

    // restore `Toolbar's` height
    mVaActionBar = ValueAnimator.ofInt(0 , mToolbarHeight);
    mVaActionBar.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            // update LayoutParams
            ((AppBarLayout.LayoutParams)mToolbar.getLayoutParams()).height
                    = (Integer)animation.getAnimatedValue();
            mToolbar.requestLayout();
        }
    });

    mVaActionBar.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation) {
            super.onAnimationStart(animation);

            if (getSupportActionBar() != null) { // sanity check
                getSupportActionBar().show();
            }
        }
    });

    mVaActionBar.setDuration(mAnimDuration);
    mVaActionBar.start();
}

在您的评论中,您提到我现在确实看到了一个动画,但在 ab.hide() 发生之前,工具栏的空间仍然保留。对我来说,这意味着您正在使用 AppBarLayout 来托管 Toolbar。如果不是这样,请告诉我,我们会想办法的。

最后,将根据以下条件分派(dispatch)对这些方法的调用:

if (getSupportActionBar().isShowing()) {
    hideActionBar();
} else {
    showActionBar();
}

关于Android SupportActionBar 在显示/隐藏时没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667552/

相关文章:

javascript - 使用 JavaScript 在三 Angular 形中查找第三个点

android - Android 2.3.5 上的 ActionBar 和 SearchView 问题

android - ActionBar 的主页图标和标题之间的填充

android - 如何在 android Activity 中绘制温度计?

java - XmlPullParser 从 START_DOCUMENT 跳转到 END_DOCUMENT 事件类型

android - 在 qt c++ 代码中创建 android.accounts.Account 的实例

swift - 重复 iCarousel 的滚动

javascript - 如何使用 Javascript 访问现有的传单 map 多段线,然后对其进行动画处理?

android - 在启动时调用 ActionBar 中的 onNavigationItemSelected 如何避免它?

android - 华为设备不会导入数据库