美文网首页
夜间模式

夜间模式

作者: TTTqiu | 来源:发表于2016-10-24 14:07 被阅读56次

1. array.xml 中设置变量

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="toolbarBackground" format="reference|color"/>
    <attr name="mainEditBackground" format="reference"/>
    <attr name="searchEditBackground" format="reference"/>
    <attr name="searchStartBackground" format="reference"/>
    <attr name="tabLayoutBackground" format="reference|color"/>
    <attr name="fragmentBackground" format="reference|color"/>
    <attr name="recyclerBackground" format="reference|color"/>
    <attr name="recyclerItemBackground" format="reference|color"/>
    <attr name="recyclerTextColor" format="reference|color"/>
    <attr name="fabBackground" format="reference|color"/>
    <attr name="navigationBackground" format="reference|color"/>
    <attr name="navigationTextColor" format="reference|color"/>
    <attr name="PreferenceBackground" format="reference|color"/>
    <attr name="nightModeSwitch" format="reference"/>
</resources>

2. style.xml 中创建白天和夜间模式的主题,分别设置变量的值

    <style name="MyTheme.NoActionBar.Day" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/purple</item>
        <item name="colorPrimaryDark">@color/purple</item>
        <item name="colorAccent">@color/yellow</item>

        <item name="toolbarBackground">@color/purple</item>
        <item name="mainEditBackground">@drawable/search_day_selector_main</item>
        <item name="searchEditBackground">@drawable/search_day_selector_search</item>
        <item name="searchStartBackground">@drawable/search_start_day_selector</item>
        <item name="tabLayoutBackground">@color/purple</item>
        <item name="fragmentBackground">@color/gray_background</item>
        <item name="recyclerBackground">@color/gray_background</item>
        <item name="recyclerItemBackground">@drawable/title_day_selector</item>
        <item name="recyclerTextColor">@color/black_title_text</item>
        <item name="fabBackground">@color/yellow</item>
        <item name="navigationBackground">@color/white</item>
        <item name="navigationTextColor">@color/black_title_text</item>
        <item name="PreferenceBackground">@color/white</item>
        <item name="nightModeSwitch">@drawable/night_mode</item>
    </style>

    <style name="MyTheme.NoActionBar.Night" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/black_night</item>
        <item name="colorPrimaryDark">@color/black_night</item>
        <item name="colorAccent">@color/gray_night</item>

        <item name="toolbarBackground">@color/black_night</item>
        <item name="mainEditBackground">@drawable/search_night_selector_main</item>
        <item name="searchEditBackground">@drawable/search_night_selector_search</item>
        <item name="searchStartBackground">@drawable/search_start_night_selector</item>
        <item name="tabLayoutBackground">@color/gray_night</item>
        <item name="fragmentBackground">@color/black_night</item>
        <item name="recyclerBackground">@color/black_night</item>
        <item name="recyclerItemBackground">@drawable/title_night_selector</item>
        <item name="recyclerTextColor">@color/white</item>
        <item name="fabBackground">@color/gray_night</item>
        <item name="navigationBackground">@color/gray_night</item>
        <item name="navigationTextColor">@color/white</item>
        <item name="PreferenceBackground">@color/gray_search_history</item>
        <item name="nightModeSwitch">@drawable/day_mode</item>
    </style>

3. 布局里用 ?attr/...... 获取当前主题的相应变量的值

如:
......
android:background="?attr/fragmentBackground"
......

4. 活动中 setTheme() 设置不同主题,注意要在 onCreate() 前

  • 一般用一个 BaseActivity 作其他活动的父类:
public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(this);
        if (pref.getBoolean("isNightMode",false)){
            setTheme(R.style.MyTheme_NoActionBar_Night);
        }else {
            setTheme(R.style.MyTheme_NoActionBar_Day);
        }

        super.onCreate(savedInstanceState);
    }
}
  • 在活动中 setTheme() 后要 reCreate(),但这样会闪屏。

5. 解决闪屏的几种可行方法

  1. 不用 setTheme(),直接给视图改变背景色等。
  2. 添加一个 View 挡在活动上面,setTheme() 结束后移除 View。
  • View 的背景可以设置成昼夜转换图片:
                nightModeView=new View(this);
                nightModeView.setBackgroundResource(R.drawable.night);
                WindowManager.LayoutParams params= new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.TYPE_APPLICATION,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                        PixelFormat.TRANSPARENT);
                windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
                windowManager.addView(nightModeView,params);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        recreate();
                    }
                }, 100);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        windowManager.removeViewImmediate(nightModeView);
                    }
                }, 1000);
  • 或使用转换前的屏幕截图做背景:
                view.setDrawingCacheEnabled(true);
                view.buildDrawingCache();
                Bitmap bitmap=view.getDrawingCache();
                BitmapDrawable bd=new BitmapDrawable(null,bitmap);
                nightModeView=new View(this);
                nightModeView.setBackground(bd);
                WindowManager.LayoutParams params= new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.TYPE_APPLICATION,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                        PixelFormat.TRANSPARENT);
                windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
                windowManager.addView(nightModeView,params);

                AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f,0.0f);
                alphaAnimation.setDuration(1000);
                nightModeView.startAnimation(alphaAnimation);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        windowManager.removeViewImmediate(nightModeView);
                        view.setDrawingCacheEnabled(false);
                    }
                },1000);

截图做背景不能用在要 reCreate() 的时候,一般用来在直接设置视图背景的情况下,同时设置 alpha 变化,使得转换很自然。

相关文章

  • 想用AppCompat的夜间模式切换功能又不想导整个库?

    目录 背景 Android中的夜间模式 AppCompat实现夜间模式 AppCompat夜间模式切换功能的分析 ...

  • 盘点win10系统里的六个神奇模式,总有一个你不知道!

    夜间模式 Win10系统加入了夜间模式,夜间模式自带护眼功能,让夜晚操作电脑的用户可以保护视力。 简单说夜间模式就...

  • Android实现夜间模式的方法(二)

    该文章接上篇 Android实现夜间模式的方法(一) 三.夜间模式的实现方案——单纯夜间模式 1.通过切换主题...

  • 微博iOS的护眼模式

    夜间模式的探讨 与其他App切换夜间模式不同: 微博采取了护眼模式: 两种方案各有利弊: 夜间模式优点:可以对每一...

  • 夜间模式

    又是一个夜晚 夜晚总会让人产生思考 这个夏天 这个暑假 找了一份暑假工兼职 算是体验生活 “享受”一下赚钱的不易 ...

  • 夜间模式

    思路: 一: 1.准备两套资源,分别对应日间模式和夜间模式。 2.在系统全局保存一个变量(BOOL isNight...

  • 夜间模式

    标签(空格分隔): Android 1、通过切换theme来实现夜间模式。2、通过资源id映射的方式来实现夜间模式...

  • 夜间模式

    夜间模式

  • 夜间模式

    夜深了,睡不着,拿着手机,息屏,开启,息屏,亮起……这样,1点,2点……这样,脑子里头绪万千

  • 夜间模式

    1. array.xml 中设置变量 2. style.xml 中创建白天和夜间模式的主题,分别设置变量的值 3....

网友评论

      本文标题:夜间模式

      本文链接:https://www.haomeiwen.com/subject/zsrluttx.html