2

我们为我们的应用程序实现了夜间模式。除了过渡之外,它就像一个魅力。我们正在使用 Base Application 类来实现它。问题是无论我们如何尝试,当配置更改时,我们都无法实现平滑过渡。

我们尝试以我们的风格实现进入和退出动画。但它适用于整个活动。所以它也会影响活动的其他转换。所以它没有用。

从图中可以看出,当配置更改时,屏幕上会出现黑色闪烁。

在此处输入图像描述

配置更改代码:

   public static void applyTheme(@NonNull String themePref) {
    switch (themePref) {
        case LIGHT_MODE: {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

            Log.d(Statics.LOG_TAG, "Applying day mode");
            break;
        }
        case DARK_MODE: {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            Log.d(Statics.LOG_TAG, "Applying night mode");
            break;
        }
        default: {
            Log.d(Statics.LOG_TAG, "Applying automatic mode");
            if (BuildCompat.isAtLeastP()) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
            }
            break;
        }
    }
}

感谢您阅读本文。任何帮助表示赞赏。

4

2 回答 2

0

请使用以下代码,它运行良好 -

//Does not work in Android Nugget
public void setDayNightMode(boolean day) {
    if (day)
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    else
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    getWindow().setWindowAnimations(R.style.WindowAnimationFadeInOut);
    recreate();
}

//Style
<style name="WindowAnimationFadeInOut">
    <item name="@android:windowEnterAnimation">@anim/fade_in</item>
    <item name="@android:windowExitAnimation">@anim/fade_out</item>
</style>



  // fade in inside anim folder 
  <set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="1000"
        android:fromAlpha="0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="1.0" />
   </set>
 
   // fade out inside anim folder
   <set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="1500"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="0" />
    </set>

我测试了它正在工作

于 2020-07-07T06:50:14.303 回答
0

设置主题后添加以下代码,对我有用

Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
于 2021-07-14T17:20:40.013 回答