如何防止状态栏和导航栏在活动场景动画过渡期间出现动画? [英] How do I prevent the status bar and navigation bar from animating during an activity scene animation transition?

查看:30
本文介绍了如何防止状态栏和导航栏在活动场景动画过渡期间出现动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的状态栏背景设置为深棕色,导航栏背景默认为黑色.我正在使用 Material 灯光主题.

Firstly, my status bar background is set to dark brown, and my navigation bar background is default black. I'm using the Material light theme.

我正在使用带有默认转换的 ActivityOptions.makeSceneTransitionAnimation 开始一个新活动,我注意到状态栏和导航栏都短暂地淡化为白色,然后又恢复为正确的颜色.

I'm starting a new activity using ActivityOptions.makeSceneTransitionAnimation with default transitions, and I notice that both the status and navigation bars briefly fade to white and then back to the correct colors.

根据文档:

要获得转换的全部效果,您必须在调用和被调用活动上启用窗口内容转换.否则,调用 Activity 将启动退出转换,但随后您将看到窗口转换(如缩放或淡入淡出)

To get the full effect of a transition, you must enable window content transitions on both the calling and called activities. Otherwise, the calling activity will start the exit transition, but then you'll see a window transition (like scale or fade)

我在调用和被调用的活动中都使用了 getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);.

I am using getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); on both the calling and called activities.

同样,如果我将输入转换更改为幻灯片,状态栏和导航栏都会短暂地出现一个带有白色背景的幻灯片转换.

Similarly, if I change the enter transition to a slide, both the status and navigation bars briefly have a slide transition with a white background.

如何防止状态栏和导航栏在活动场景动画过渡期间出现动画?

How do I prevent the status bar and navigation bar from animating during an activity scene animation transition?

推荐答案

据我所知,您可以使用两种方法来防止导航/状态栏在过渡期间出现动画:

There are two approaches you can use that I know of to prevent the navigation/status bar from animating during the transition:

导航/状态栏在过渡期间淡入和淡出的原因是因为默认情况下所有非共享视图(包括导航/状态栏背景)将分别在您的调用/被调用活动中淡入/淡入一次过渡开始.但是,您可以通过从窗口的默认退出/进入 Fade 转换中排除导航/状态栏背景来轻松解决此问题.只需将以下代码添加到您的活动的 onCreate() 方法中:

The reason why the navigation/status bar are fading in and out during the transition is because by default all non-shared views (including the navigation/status bar backgrounds) will fade out/in in your calling/called Activitys respectively once the transition begins. You can, however, easily get around this by excluding the navigation/status bar backgrounds from the window's default exit/enter Fade transition. Simply add the following code to your Activitys' onCreate() methods:

Transition fade = new Fade();
fade.excludeTarget(android.R.id.statusBarBackground, true);
fade.excludeTarget(android.R.id.navigationBarBackground, true);
getWindow().setExitTransition(fade);
getWindow().setEnterTransition(fade);

也可以使用 XML 在活动的主题中声明此转换(即在您自己的 res/transition/window_fade.xml 文件中):

This transition could also be declared in the activity's theme using XML (i.e. in your own res/transition/window_fade.xml file):

<?xml version="1.0" encoding="utf-8"?>
<fade xmlns:android="http://schemas.android.com/apk/res/android">
    <targets>
        <target android:excludeId="@android:id/statusBarBackground"/>
        <target android:excludeId="@android:id/navigationBarBackground"/>
    </targets>
</fade>

方法#2:将状态栏和导航栏添加为共享元素

这种方法建立在 klmprt 的答案之上,它几乎对我有用......尽管我仍然需要进行一些修改.

Approach #2: Add the status bar and navigation bar as shared elements

This approach builds off of klmprt's answer, which almost worked for me... although I still needed to make a couple of modifications.

在我的调用 Activity 中,我使用以下代码来启动 Activity:

In my calling Activity, I used the following code to start the Activity:

View statusBar = findViewById(android.R.id.statusBarBackground);
View navigationBar = findViewById(android.R.id.navigationBarBackground);

List<Pair<View, String>> pairs = new ArrayList<>();
if (statusBar != null) {
  pairs.add(Pair.create(statusBar, Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME));
}
if (navigationBar != null) {
  pairs.add(Pair.create(navigationBar, Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME));
}
pairs.add(Pair.create(mSharedElement, mSharedElement.getTransitionName()));

Bundle options = ActivityOptions.makeSceneTransitionAnimation(activity, 
        pairs.toArray(new Pair[pairs.size()])).toBundle();
startActivity(new Intent(context, NextActivity.class), options);

到目前为止,这与 klmprt 在他的回答中建议的内容基本相同.但是,我还需要在我调用的 Activity 的 onCreate() 方法中添加以下代码,以防止状态栏和导航栏在转换过程中闪烁":

So far this is essentially the same thing that klmprt suggested in his answer. However, I also needed to add the following code in my called Activity's onCreate() method in order to prevent the status bar and navigation bar from "blinking" during the transition:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);

    // Postpone the transition until the window's decor view has
    // finished its layout.
    postponeEnterTransition();

    final View decor = getWindow().getDecorView();
    decor.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            decor.getViewTreeObserver().removeOnPreDrawListener(this);
            startPostponedEnterTransition();
            return true;
        }
    });
}

将状态栏和导航栏背景添加为共享元素将强制它们绘制在窗口默认退出/进入淡入淡出过渡的顶部,这意味着它们不会在过渡期间淡出.关于这种方法的更多讨论可以在这篇 Google+ 帖子中找到.

Adding the status bar and navigation bar backgrounds as shared elements will force them to be drawn on top of the window's default exit/enter fade transition, meaning that they will not fade during the transition. More discussion about this approach can be found in this Google+ post.

这篇关于如何防止状态栏和导航栏在活动场景动画过渡期间出现动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆