https://zhuanlan.zhihu.com/p/159219944
三種高性價(jià)比的Android 夜間模式實(shí)現(xiàn),用過的都說好!
執(zhí)著、向上
2 人贊同了該文章
主題方式
這是最正統(tǒng)的方式,但工作量巨大,因?yàn)橐痔鎿Q xml 布局中所有硬編碼的色值,將其換成主題色。然后通過換主題達(dá)到換膚的效果。
作者:唐子玄
鏈接:https://juejin.im/post/5f07d6bff265da22a8515691
窗口方式
是不是可以在所有界面上罩一個(gè)半透明的窗口,就好像戴墨鏡看屏幕一樣。雖然這是換膚方案的“退而求其次”,但也是能達(dá)到不刺眼的效果:
open class BaseActivity : AppCompatActivity() {
? ? // 展示全局半透明浮窗
? ? private fun showMaskWindow() {
? ? ? ? // 浮窗內(nèi)容?
? ? ? ? val view = View {
? ? ? ? ? ? layout_width = match_parent
? ? ? ? ? ? layout_height = match_parent
? ? ? ? ? ? background_color = "#c8000000"
? ? ? ? }
? ? ? ? val windowInfo = FloatWindow.WindowInfo(view).apply {
? ? ? ? ? ? width = DimensionUtil.getScreenWidth(this@BaseActivity)
? ? ? ? ? ? height = DimensionUtil.getScreenHeight(this@BaseActivity)
? ? ? ? }
? ? ? ? // 展示浮窗
? ? ? ? FloatWindow.show(this, "mask", windowInfo, 0, 100, false, false, true)
? ? }
}
其中的View{},是構(gòu)建布局的 DSL,它實(shí)例化了一個(gè)半透明的View,詳細(xì)講解可以點(diǎn)擊這里。
其中FloatWindow,是浮窗管理類,show()方法會(huì)向界面中添加一個(gè)全局Window,詳細(xì)講解可以點(diǎn)擊這里。
為了讓浮窗跨Activity展示,需要將窗口的type設(shè)置為WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY。
為了讓觸摸事件穿透浮窗傳遞到Activity,需要為窗口添加下面這幾個(gè)flag,F(xiàn)LAG_NOT_FOCUSABLE、FLAG_NOT_TOUCHABLE、FLAG_NOT_TOUCH_MODAL、FLAG_FULLSCREEN。
這些細(xì)節(jié)已封裝在FloatWindow中。
這個(gè)方案有一個(gè)缺點(diǎn),當(dāng)展示系統(tǒng)多任務(wù)時(shí),全局浮窗會(huì)消失,效果如下:

視圖方式
是不是可以向每個(gè)當(dāng)前界面添加一個(gè)半透明的View作為蒙版?
fun Activity.nightMode(lightOff: Boolean, color: String) {
? ? // 構(gòu)建主線程消息處理器
? ? val handler = Handler(Looper.getMainLooper())
? ? // 蒙版控件id
? ? val id = "darkMask"
? ? // 打開夜間模式
? ? if (lightOff) {
? ? ? ? // 向主線程消息隊(duì)列頭部插入“展示蒙版”任務(wù)
? ? ? ? handler.postAtFrontOfQueue {
? ? ? ? ? ? // 構(gòu)建蒙版視圖
? ? ? ? ? ? val maskView = View {
? ? ? ? ? ? ? ? layout_id = id
? ? ? ? ? ? ? ? layout_width = match_parent
? ? ? ? ? ? ? ? layout_height = match_parent
? ? ? ? ? ? ? ? background_color = color
? ? ? ? ? ? }
? ? ? ? ? ? // 向當(dāng)前界面頂層視圖中添加蒙版視圖
? ? ? ? ? ? decorView?.apply {
? ? ? ? ? ? ? ? val view = findViewById<View>(id.toLayoutId())
? ? ? ? ? ? ? ? if (view == null) { addView(maskView) }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? // 關(guān)閉夜間模式
? ? else {
? ? ? ? // 從當(dāng)前界面頂層視圖中移出蒙版視圖
? ? ? ? decorView?.apply {
? ? ? ? ? ? find<View>(id)?.let { removeView(it) }
? ? ? ? }
? ? }
}
為AppCompatActivity擴(kuò)展了一個(gè)方法,它用于開關(guān)夜間模式。打開夜間模式的方式是?“向當(dāng)前界面頂層視圖添加一個(gè)蒙版視圖”?。
其中decorView是Activity的一個(gè)擴(kuò)展屬性:
val Activity.decorView: FrameLayout?
? ? get() = (takeIf { !isFinishing && !isDestroyed }?.window?.decorView) as? FrameLayout
當(dāng)Activity還展示的時(shí)候,從它的Window中獲取DecorView。
其中toLayoutId()是String的擴(kuò)展方法:
fun String.toLayoutId(): Int {
? ? var id = java.lang.String(this).bytes.sum()
? ? if (id == 48) id = 0
? ? return id
}
它將String轉(zhuǎn)化成Int值,算法是將String先轉(zhuǎn)換成字節(jié),然后將所有字節(jié)累加,詳細(xì)介紹可以點(diǎn)擊這里。
為了避免界面展示出來后黑一下,所以將“添加蒙版”任務(wù)添加到主線程消息隊(duì)列的頭部,優(yōu)先處理。
然后只需在Application中監(jiān)聽Activity的生命周期,在onCreate()中開關(guān)夜間模式即可:
class TaylorApplication : Application() {
? ? private val preference by lazy { Preference(getSharedPreferences("dark-mode", Context.MODE_PRIVATE)) }
? ? override fun onCreate() {
? ? ? ? super.onCreate()
? ? ? ? registerActivityLifecycleCallbacks(object :ActivityLifecycleCallbacks{
? ? ? ? ? ? override fun onActivityPaused(activity: Activity?) {}
? ? ? ? ? ? override fun onActivityResumed(activity: Activity?) {}
? ? ? ? ? ? override fun onActivityStarted(activity: Activity?) {}
? ? ? ? ? ? override fun onActivityDestroyed(activity: Activity?) {}
? ? ? ? ? ? override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {}
? ? ? ? ? ? override fun onActivityStopped(activity: Activity?) {}
? ? ? ? ? ? override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
? ? ? ? ? ? ? ? activity?.night(preference["dark-mode", false])
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
其中Preference是對SharedPreference的封裝,它用更簡潔的語法實(shí)現(xiàn)值的存取,且可以忽略類型,詳細(xì)介紹可以點(diǎn)擊這里。
效果如下:

這個(gè)方案不是全局的,而是針對單界面的,所以彈出的DialogFragment會(huì)在蒙版之上,那就用同樣的方法在對話框上再覆蓋一層蒙版:
fun DialogFragment.nightMode(lightOff: Boolean, color: String = "#c8000000") {
? ? val handler = Handler(Looper.getMainLooper())
? ? val id = "darkMask"
? ? if (lightOff) {
? ? ? ? handler.postAtFrontOfQueue {
? ? ? ? ? ? val maskView = View {
? ? ? ? ? ? ? ? layout_id = id
? ? ? ? ? ? ? ? layout_width = match_parent
? ? ? ? ? ? ? ? layout_height = match_parent
? ? ? ? ? ? ? ? background_color = color
? ? ? ? ? ? }
? ? ? ? ? ? decorView?.apply {
? ? ? ? ? ? ? ? val view = findViewById<View>(id.toLayoutId())
? ? ? ? ? ? ? ? if (view == null) {
? ? ? ? ? ? ? ? ? ? addView(maskView)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? } else {
? ? ? ? decorView?.apply {
? ? ? ? ? ? find<View>(id)?.let { removeView(it) }
? ? ? ? }
? ? }
}
// 獲取對話框的根視圖
val DialogFragment.decorView: ViewGroup?
? ? get() {
? ? ? ? return view?.parent as? ViewGroup
? ? }
添加蒙版的算法和之前的一摸一樣,只不過這次是DialogFragment的擴(kuò)展方法。
更多系列教程GitHub白嫖入口:https://github.com/Timdk857/Android-Architecture-knowledge-2-
B站全套Android移動(dòng)架構(gòu)師進(jìn)階視頻教程白嫖地址:https://space.bilibili.com/544650554
