我如何prevent从活动现场的动画过渡过程中动态显示状态栏和导航栏? [英] How do I prevent the status bar and navigation bar from animating during an activity scene animation transition?

查看:242
本文介绍了我如何prevent从活动现场的动画过渡过程中动态显示状态栏和导航栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的状态栏背景设置为黑褐色,和我的导航栏的背景为默认的黑色。我使用的材料轻的主题。

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.

按照文档

要得到一个过渡的完整效果,您必须启用同时在主叫和被叫的活动窗口的内容转换。否则,调用活动将启动退出缓和,但随后你会看到一个窗口的过渡(如规模或褪色)

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.

我如何prevent从活动现场的动画过渡过程中动态显示状态栏和导航栏?

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

推荐答案

有两种方法可以使用​​,我所知道的prevent导航/状态栏在过渡期间的动画:

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

为什么导航/状态栏的淡入淡出过渡时期的原因是因为默认情况下,所有非共享的意见(包括导航/状态栏背景)将淡出/在你的主/被叫Activitys分别一次转型开始。你可以,但是,很容易解决这个问题通过排除导航/状态栏的背景从窗口的默认退出/进入淡入淡出过渡。只需添加以下code您Activitys的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 /转换/ 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.

在我的呼唤活动,我用下面的code键启动活动:

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<>();
pairs.add(Pair.create(statusBar, Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME));
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建议在他的回答。不过,我也需要为了从眨眼添加以下code在我所谓的活动的的onCreate()方法prevent状态栏和导航栏在过渡期间:

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.

这篇关于我如何prevent从活动现场的动画过渡过程中动态显示状态栏和导航栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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