Android development black and white gray mode and night mode settings

The source of the interface data is Hongyang Dashen's "playing Android" website: https://wanandroid.com/

Black and white gray normal mode and black and white gray night mode screenshots

  

Night mode and normal mode screenshots

    

Black and white gray and primary color mode settings

    /**
     * 设置灰白色
     */
    protected void setGrayScreen() {
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        getWindow().getDecorView().setLayerType(View.LAYER_TYPE_HARDWARE, paint);
    }

    /**
     * 设置原色,灰白色、原色都不设置默认为原色
     */
    protected void setOriginalScreen() {
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(1);
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        getWindow().getDecorView().setLayerType(View.LAYER_TYPE_HARDWARE, paint);
    }

 use

 First write the above method in BaseActivity, and other activities inherit the BaseActivity class. Then call the global method according to your own business logic.

Night mode and day mode settings

To set the night mode, you first need to set the parent attribute in the styles.xml file under the values ​​folder to: Theme.AppCompat.DayNight.NoActionBar

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

Then you need to configure the color values ​​​​needed by the day mode in the colors.xml file under the values ​​folder

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>

    <color name="colorBg">#F4F5F7</color>
    <color name="white">#ffffff</color>
    <color name="black">#000000</color>
</resources>

Configure the color value corresponding to the night mode in colors.xml under the values-night folder

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#303030</color>
    <color name="colorPrimaryDark">#232323</color>
    <color name="colorAccent">#008577</color>

    <color name="white">#232323</color>
    <color name="black">#ffffff</color>
</resources>

Finally, just call the public method in BaseActivity according to your own business logic.

    /**
     * 设置夜间模式
     */
    protected void setModeNight() {
        getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

    /**
     * 设置白天模式,白天、夜间都不设置跟随系统
     */
    protected void setModeLight() {
        getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }

Judgment between night mode and day mode

int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;

if (mode == Configuration.UI_MODE_NIGHT_YES) {//夜间模式
                    
} else if (mode == Configuration.UI_MODE_NIGHT_NO) {//非夜间模式
                   
}

//根据判断重新设置模式后刷新使设置生效
recreate();

Code details: https://gitee.com/juer2017/MVP_Demo

Guess you like

Origin blog.csdn.net/juer2017/article/details/128288533