android-夜间模式

资源

1

Android Material Design系列之夜间模式
阐述了夜间模式的资源文件,告知建立了values-night文件夹

对于夜间模式的颜色和主题配置,我们需要建立一个res下建立一个values-night文件夹,里面放着夜间主题样式的color等资源。
colors.xml配置如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#0a0a0a</color>
    <color name="colorPrimaryDark">#000000</color>
    <color name="colorAccent">#fc0404</color>
    <color name="add_bg_color">#FF2ECC71</color>
    <color name="add_selected_color">#51C332</color>
    <color name="white">#ffffff</color>
    <color name="text_color">#7f7f7f</color>
    <color name="navigation_bar_color">#0a0a0a</color>
    <color name="color_control_normal">#f305be</color>
    <color name="text_color_primary">#00ffff</color>
    <color name="navigationview_bg_color">#000000</color>
</resources>

切换主体

isNight = sp.getBoolean("night", false);
if (isNight) {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    sp.edit().putBoolean("night", false).commit();
} else {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    sp.edit().putBoolean("night", true).commit();
}
recreate();

2. 夜间模式工具类

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;

import java.lang.ref.WeakReference;

/**
 * Night Mode Helper
 * <p>
 * Helps use utilise the night and notnight resource qualifiers without being in car or dock mode.
 * <p>
 * Implementation is simple. Add the follow line at the top of your activity's onCreate just after
 * the super.onCreate(); The idea here is to do it before we create any views. So the new views will
 * use the correct Configuration.
 * 
 * <pre>
 * mNightModeHelper = new NightModeHelper(this, R.style.AppTheme);
 * </pre>
 * 
 * You can now use your instance of NightModeHelper to control which mode you are in. You can choose
 * to persist the current setting and hand it back to this class as the defaultUiMode, otherwise
 * this is done for you automatically.
 * <p>
 * I'd suggest you setup your Theme as follows:
 * <ul>
 * <li><b>res\values\styles.xml</b>
 * 
 * <pre>
 * &lt;style name=&quot;AppTheme&quot; parent=&quot;AppBaseTheme&quot;&gt;&lt;/style&gt;
 * </pre>
 * 
 * </li>
 * <li><b>res\values-night\styles.xml</b>
 * 
 * <pre>
 * &lt;style name=&quot;AppBaseTheme&quot; parent=&quot;@android:style/Theme.Holo&quot;&gt;&lt;/style&gt;
 * </pre>
 * 
 * </li>
 * <li><b>res\values-notnight\styles.xml</b>
 * 
 * <pre>
 * &lt;style name=&quot;AppBaseTheme&quot; parent=&quot;@android:style/Theme.Holo.Light&quot;&gt;&lt;/style&gt;
 * </pre>
 * 
 * </li>
 * </ul>
 * 
 * @author Simon Lightfoot <[email protected]>
 */
public class NightModeHelper {
    private static final String PREF_KEY = "nightModeState";

    private static int sUiNightMode = Configuration.UI_MODE_NIGHT_UNDEFINED;

    private WeakReference<Activity> mActivity;
    private SharedPreferences mPrefs;

    /**
     * Default behaviour is to automatically save the setting and restore it.
     */
    public NightModeHelper(Activity activity, int theme) {
        int currentMode = (activity.getResources().getConfiguration().uiMode
                & Configuration.UI_MODE_NIGHT_MASK);
        mPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
        init(activity, theme, mPrefs.getInt(PREF_KEY, currentMode));
    }

    /**
     * If you don't want the autoSave feature and instead want to provide your own persisted storage
     * for the mode, use the defaultUiMode for it.
     */
    public NightModeHelper(Activity activity, int theme, int defaultUiMode) {
        init(activity, theme, defaultUiMode);
    }

    public static int getUiNightMode() {
        return sUiNightMode;
    }

    private void init(Activity activity, int theme, int defaultUiMode) {
        mActivity = new WeakReference<Activity>(activity);
        if (sUiNightMode == Configuration.UI_MODE_NIGHT_UNDEFINED) {
            sUiNightMode = defaultUiMode;
        }
        updateConfig(sUiNightMode);

        // This may seem pointless but it forces the Theme to be reloaded
        // with new styles that would change due to new Configuration.
        activity.setTheme(theme);
    }

    private void updateConfig(int uiNightMode) {
        Activity activity = mActivity.get();
        if (activity == null) {
            throw new IllegalStateException("Activity went away?");
        }
        Configuration newConfig = new Configuration(activity.getResources().getConfiguration());
        newConfig.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        newConfig.uiMode |= uiNightMode;
        activity.getResources().updateConfiguration(newConfig, null);
        sUiNightMode = uiNightMode;
        if (mPrefs != null) {
            mPrefs.edit()
                    .putInt(PREF_KEY, sUiNightMode)
                    .apply();
        }
    }

    public void toggle() {
        if (sUiNightMode == Configuration.UI_MODE_NIGHT_YES) {
            notNight();
        } else {
            night();
        }
    }

    public void notNight() {
        updateConfig(Configuration.UI_MODE_NIGHT_NO);
        mActivity.get().recreate();
    }

    public void night() {
        updateConfig(Configuration.UI_MODE_NIGHT_YES);
        mActivity.get().recreate();
    }
}

3.Android Support Library 之 夜间模式

Android Support Library 之 夜间模式

4.android夜间模式浅析

android夜间模式浅析

  1. 定义DayNight主体,以及相关属性
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
      <!-- Customize your theme here. -->
      <item name="colorPrimary">@color/colorPrimary</item>
      <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
      <item name="colorAccent">@color/colorAccent</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="colorPrimary">#3F51B5</color>
   <color name="colorPrimaryDark">#303F9F</color>
   <color name="colorAccent">#FF4081</color>
</resources>
  1. 在res下面创建values-night文件夹,创建colors.xml声明color属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="colorPrimary">#1f2023</color>
   <color name="colorPrimaryDark">#18181a</color>
   <color name="colorAccent">#FF4081</color>
</resources>
  1. 为程序设置初始模式
MODE_NIGHT_NO. 使用亮色(light)主题

MODE_NIGHT_YES. 使用暗色(dark)主题

MODE_NIGHT_AUTO. 根据当前时间自动切换 亮色(light)/暗色(dark)主题

MODE_NIGHT_FOLLOW_SYSTEM(默认选项). 设置为跟随系统,通常为 MODE_NIGHT_NO
  1. 在Application中进行初始化
public class MyApplication extends Application {
	static {
	    AppCompatDelegate.setDefaultNightMode(
	            AppCompatDelegate.MODE_NIGHT_NO);
	}
	
	@Override
	public void onCreate() {
	    super.onCreate();
	}

}
  1. 在初始化就切换夜间模式
public class MyApplication extends Application {
static {
    AppCompatDelegate.setDefaultNightMode(
            AppCompatDelegate.MODE_NIGHT_NO);
}

@Override
public void onCreate() {
    super.onCreate();
}

}
  1. 获取当前主体的状态
int currentNightMode = getResources().getConfiguration().uiMode
    & Configuration.UI_MODE_NIGHT_MASK;

case Configuration.UI_MODE_NIGHT_NO:

case Configuration.UI_MODE_NIGHT_YES:

case Configuration.UI_MODE_NIGHT_UNDEFINED:
  1. 通过主体确定
    int currentNightMode = getResources().getConfiguration().uiMode
            & Configuration.UI_MODE_NIGHT_MASK;
    switch (currentNightMode) {
        case Configuration.UI_MODE_NIGHT_NO:
            getDelegate().setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_YES);


            break;
        case Configuration.UI_MODE_NIGHT_YES:
             getDelegate().setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_NO);
            break;
        case Configuration.UI_MODE_NIGHT_UNDEFINED:
            getDelegate().setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_AUTO);
            break;
    }
    // 调用 recreate() 使设置生效
    recreate();
}
  1. 图片文件夹
    drawable-night
发布了167 篇原创文章 · 获赞 62 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/AdrianAndroid/article/details/100702441
今日推荐