|
1 | 1 | package com.clock.study.activity; |
2 | 2 |
|
3 | | -import android.animation.Animator; |
4 | | -import android.animation.AnimatorListenerAdapter; |
5 | | -import android.animation.ObjectAnimator; |
6 | | -import android.graphics.Bitmap; |
| 3 | +import android.content.res.Resources; |
7 | 4 | import android.os.Bundle; |
8 | 5 | import android.support.v7.app.AppCompatActivity; |
9 | | -import android.view.Menu; |
10 | | -import android.view.MenuInflater; |
11 | | -import android.view.MenuItem; |
12 | | -import android.view.View; |
13 | | -import android.widget.ImageView; |
| 6 | +import android.support.v7.widget.LinearLayoutManager; |
| 7 | +import android.support.v7.widget.RecyclerView; |
| 8 | +import android.util.Log; |
| 9 | +import android.util.TypedValue; |
| 10 | +import android.view.Window; |
| 11 | +import android.widget.CheckBox; |
| 12 | +import android.widget.CompoundButton; |
| 13 | +import android.widget.LinearLayout; |
| 14 | +import android.widget.RelativeLayout; |
| 15 | +import android.widget.TextView; |
14 | 16 |
|
15 | 17 | import com.clock.study.R; |
| 18 | +import com.clock.study.adapter.SimpleAuthorAdapter; |
| 19 | +import com.clock.study.helper.DayNightHelper; |
| 20 | +import com.clock.study.type.DayNight; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
16 | 24 |
|
17 | 25 | /** |
18 | 26 | * 夜间模式实现方案 |
19 | 27 | * |
20 | 28 | * @author Clock |
21 | | - * @since 2016=08-11 |
| 29 | + * @since 2016-08-11 |
22 | 30 | */ |
23 | | -public class DayNightActivity extends AppCompatActivity { |
| 31 | +public class DayNightActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { |
24 | 32 |
|
25 | 33 | private final static String TAG = DayNightActivity.class.getSimpleName(); |
26 | 34 |
|
27 | | - private static boolean isNight = false; |
| 35 | + private DayNightHelper mDayNightHelper; |
28 | 36 |
|
29 | | - private View mRootView; |
30 | | - /** |
31 | | - * 切换模式时,保存界面效果用于 |
32 | | - */ |
33 | | - private ImageView mTransView; |
| 37 | + private RecyclerView mRecyclerView; |
| 38 | + |
| 39 | + private LinearLayout mHeaderLayout; |
| 40 | + private List<RelativeLayout> mLayoutList; |
| 41 | + private List<TextView> mTextViewList; |
| 42 | + private List<CheckBox> mCheckBoxList; |
34 | 43 |
|
35 | 44 | @Override |
36 | 45 | protected void onCreate(Bundle savedInstanceState) { |
37 | 46 | super.onCreate(savedInstanceState); |
38 | | - if (isNight) { |
39 | | - setTheme(R.style.NightTheme); |
40 | | - } else { |
41 | | - setTheme(R.style.DayTheme); |
42 | | - } |
| 47 | + supportRequestWindowFeature(Window.FEATURE_NO_TITLE); |
| 48 | + initData(); |
| 49 | + initTheme(); |
43 | 50 | setContentView(R.layout.activity_day_night); |
44 | 51 | initView(); |
| 52 | + } |
| 53 | + |
| 54 | + private void initView() { |
| 55 | + mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); |
| 56 | + RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); |
| 57 | + mRecyclerView.setLayoutManager(layoutManager); |
| 58 | + mRecyclerView.setAdapter(new SimpleAuthorAdapter()); |
| 59 | + |
| 60 | + mHeaderLayout = (LinearLayout) findViewById(R.id.header_layout); |
| 61 | + |
| 62 | + mLayoutList = new ArrayList<>(); |
| 63 | + mLayoutList.add((RelativeLayout) findViewById(R.id.jianshu_layout)); |
| 64 | + mLayoutList.add((RelativeLayout) findViewById(R.id.zhihu_layout)); |
| 65 | + |
| 66 | + mTextViewList = new ArrayList<>(); |
| 67 | + mTextViewList.add((TextView) findViewById(R.id.tv_jianshu)); |
| 68 | + mTextViewList.add((TextView) findViewById(R.id.tv_zhihu)); |
| 69 | + |
| 70 | + mCheckBoxList = new ArrayList<>(); |
| 71 | + CheckBox ckbJianshu = (CheckBox) findViewById(R.id.ckb_jianshu); |
| 72 | + ckbJianshu.setOnCheckedChangeListener(this); |
| 73 | + mCheckBoxList.add(ckbJianshu); |
| 74 | + CheckBox ckbZhihu = (CheckBox) findViewById(R.id.ckb_zhihu); |
| 75 | + ckbZhihu.setOnCheckedChangeListener(this); |
| 76 | + mCheckBoxList.add(ckbZhihu); |
45 | 77 |
|
46 | 78 | } |
47 | 79 |
|
48 | | - @Override |
49 | | - public boolean onCreateOptionsMenu(Menu menu) { |
50 | | - MenuInflater menuInflater = new MenuInflater(this); |
51 | | - menuInflater.inflate(R.menu.day_night_menu, menu); |
52 | | - return super.onCreateOptionsMenu(menu); |
| 80 | + private void initData() { |
| 81 | + mDayNightHelper = new DayNightHelper(this); |
53 | 82 | } |
54 | 83 |
|
| 84 | + |
55 | 85 | @Override |
56 | | - public boolean onOptionsItemSelected(MenuItem item) { |
57 | | - int itemId = item.getItemId(); |
58 | | - if (itemId == R.id.day_mode) { |
59 | | - isNight = false; |
60 | | - recreate(); |
61 | | - } else if (itemId == R.id.night_mode) { |
62 | | - isNight = true; |
63 | | - //initTransBitmap(); |
64 | | - //startAnimation(mTransView); |
65 | | - recreate(); |
| 86 | + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 87 | + int viewId = buttonView.getId(); |
| 88 | + if (viewId == R.id.ckb_jianshu) { |
| 89 | + changeThemeByJianShu(); |
| 90 | + |
| 91 | + } else if (viewId == R.id.ckb_zhihu) { |
| 92 | + changeThemeByZhiHu(); |
| 93 | + |
66 | 94 | } |
67 | | - return super.onOptionsItemSelected(item); |
68 | 95 | } |
69 | 96 |
|
70 | | - private void initView() { |
71 | | - mRootView = findViewById(R.id.layout_main); |
72 | | - mTransView = (ImageView) findViewById(R.id.iv_trans); |
| 97 | + private void initTheme() { |
| 98 | + if (mDayNightHelper.isDay()) { |
| 99 | + setTheme(R.style.DayTheme); |
| 100 | + } else { |
| 101 | + setTheme(R.style.NightTheme); |
| 102 | + } |
73 | 103 | } |
74 | 104 |
|
75 | 105 | /** |
76 | | - * 初始化起过渡效果用的Bitmap |
| 106 | + * 切换主题设置 |
77 | 107 | */ |
78 | | - private void initTransBitmap() { |
79 | | - //开启绘制缓存,让View中的内容能够转换成为Bitmap |
80 | | - mRootView.setDrawingCacheEnabled(true); |
81 | | - Bitmap bitmap = mRootView.getDrawingCache(); |
82 | | - if (bitmap != null) { |
83 | | - mTransView.setImageBitmap(bitmap); |
84 | | - mTransView.setAlpha(1f); |
85 | | - mTransView.setVisibility(View.VISIBLE); |
| 108 | + private void toggleThemeSetting() { |
| 109 | + if (mDayNightHelper.isDay()) { |
| 110 | + mDayNightHelper.setMode(DayNight.NIGHT); |
| 111 | + setTheme(R.style.NightTheme); |
| 112 | + } else { |
| 113 | + mDayNightHelper.setMode(DayNight.DAY); |
| 114 | + setTheme(R.style.DayTheme); |
86 | 115 | } |
87 | 116 | } |
88 | 117 |
|
89 | 118 | /** |
90 | | - * 启动过度用的动画效果 |
| 119 | + * 使用简书的实现套路来切换夜间主题 |
91 | 120 | */ |
92 | | - private void startAnimation(final View transView) { |
93 | | - ObjectAnimator animator = ObjectAnimator.ofFloat(transView, "alpha", 1f, 0f); |
94 | | - animator.addListener(new AnimatorListenerAdapter() { |
95 | | - @Override |
96 | | - public void onAnimationEnd(Animator animation) { |
97 | | - super.onAnimationEnd(animation); |
98 | | - transView.setVisibility(View.GONE); |
99 | | - } |
100 | | - }); |
101 | | - animator.setDuration(1000); |
102 | | - animator.start(); |
| 121 | + private void changeThemeByJianShu() { |
| 122 | + toggleThemeSetting(); |
103 | 123 |
|
| 124 | + final TypedValue typedValue = new TypedValue(); |
| 125 | + Resources.Theme theme = getTheme(); |
| 126 | + |
| 127 | + theme.resolveAttribute(R.attr.clockBackground, typedValue, true); |
| 128 | + Log.i(TAG, "clockBackground resourceId: " + typedValue.resourceId); |
| 129 | + mHeaderLayout.setBackgroundResource(typedValue.resourceId); |
| 130 | + mRecyclerView.setBackgroundResource(typedValue.resourceId); |
| 131 | + for (RelativeLayout layout : mLayoutList) { |
| 132 | + layout.setBackgroundResource(typedValue.resourceId); |
| 133 | + } |
| 134 | + for (CheckBox checkBox : mCheckBoxList) { |
| 135 | + checkBox.setBackgroundResource(typedValue.resourceId); |
| 136 | + } |
| 137 | + for (TextView textView : mTextViewList) { |
| 138 | + textView.setBackgroundResource(typedValue.resourceId); |
| 139 | + } |
| 140 | + |
| 141 | + theme.resolveAttribute(R.attr.clockTextColor, typedValue, true); |
| 142 | + Log.i(TAG, "clockTextColor resourceId: " + typedValue.resourceId); |
| 143 | + for (TextView textView : mTextViewList) { |
| 144 | + textView.setTextColor(getResources().getColor(typedValue.resourceId)); |
| 145 | + } |
104 | 146 | } |
105 | 147 |
|
| 148 | + /** |
| 149 | + * 使用知乎的实现套路来切换夜间主题 |
| 150 | + */ |
| 151 | + private void changeThemeByZhiHu() { |
| 152 | + toggleThemeSetting(); |
| 153 | + } |
106 | 154 | } |
0 commit comments