夜間模式

1. array.xml 中設(shè)置變量

<?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 中創(chuàng)建白天和夜間模式的主題,分別設(shè)置變量的值

    <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/...... 獲取當(dāng)前主題的相應(yīng)變量的值

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

4. 活動(dòng)中 setTheme() 設(shè)置不同主題,注意要在 onCreate() 前

  • 一般用一個(gè) BaseActivity 作其他活動(dòng)的父類:
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);
    }
}
  • 在活動(dòng)中 setTheme() 后要 reCreate(),但這樣會(huì)閃屏。

5. 解決閃屏的幾種可行方法

  1. 不用 setTheme(),直接給視圖改變背景色等。
  2. 添加一個(gè) View 擋在活動(dòng)上面,setTheme() 結(jié)束后移除 View。
  • View 的背景可以設(shè)置成晝夜轉(zhuǎn)換圖片:
                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);
  • 或使用轉(zhuǎn)換前的屏幕截圖做背景:
                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() 的時(shí)候,一般用來(lái)在直接設(shè)置視圖背景的情況下,同時(shí)設(shè)置 alpha 變化,使得轉(zhuǎn)換很自然。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容