如何处理夜间模式的转换?

3
我们在应用程序中实现了夜间模式。它的功能非常好,除了转换时出现的问题。我们使用Base Application类来实现它。问题是,无论我们尝试什么,当配置更改时,我们都无法实现平滑的过渡。
我们尝试在样式中实现进入和退出动画。但它适用于整个活动。因此,它还会影响活动的其他转换。所以它没有起作用。
从图像中可以看出,在配置更改时屏幕上会出现黑色闪烁。

enter image description here

配置更改代码:

   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;
        }
    }
}

感谢阅读此内容。任何帮助都将不胜感激。

1
你有这个问题的解决方案吗?如果有,请也告诉我一下。我也遇到了同样的问题。 - Dheeraj Singh Bhadoria
我找不到平滑过渡的解决方案。如果你找到了,请分享结果 :) - Eren Tüfekçi
3个回答

1
请使用以下代码,它完美地运行 -
//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>

我测试过了,它可以工作。


1
完全无法在Android 11上工作,它只是在没有任何过渡的情况下更改了暗模式。 - Shadow

0
在设置主题之后,添加以下代码,对我有效。
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);

0

day_night=findViewById(R.id.day_night);
    day_night.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
            else {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            }
        }
    });```

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接