實現(xiàn)夜間模式有很多種方式,經(jīng)過多次嘗試,算是找到了一種性價比較高的方式。
主題方式
這是最正統(tǒng)的方式,但工作量巨大,因為要全局替換 xml 布局中所有硬編碼的色值,將其換成主題色。然后通過換主題達(dá)到換膚的效果。
窗口方式
是不是可以在所有界面上罩一個半透明的窗口,就好像戴墨鏡看屏幕一樣。雖然這是換膚方案的“退而求其次”,但也是能達(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,它實例化了一個半透明的View,
其中FloatWindow,是浮窗管理類,show()方法會向界面中添加一個全局Window
為了讓浮窗跨Activity展示,需要將窗口的type設(shè)置為WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY。
為了讓觸摸事件穿透浮窗傳遞到Activity,需要為窗口添加下面這幾個flag,F(xiàn)LAG_NOT_FOCUSABLE、FLAG_NOT_TOUCHABLE、FLAG_NOT_TOUCH_MODAL、FLAG_FULLSCREEN。
這些細(xì)節(jié)已封裝在FloatWindow中。
這個方案有一個缺點,當(dāng)展示系統(tǒng)多任務(wù)時,全局浮窗會消失,效果如下:
視圖方式
是不是可以向每個當(dāng)前界面添加一個半透明的View作為蒙版?
fun Activity.nightMode(lightOff: Boolean, color: String) {
? ? // 構(gòu)建主線程消息處理器
? ? val handler = Handler(Looper.getMainLooper())
? ? // 蒙版控件id
? ? val id = "darkMask"
? ? // 打開夜間模式
? ? if (lightOff) {
? ? ? ? // 向主線程消息隊列頭部插入“展示蒙版”任務(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ò)展了一個方法,它用于開關(guān)夜間模式。打開夜間模式的方式是 “向當(dāng)前界面頂層視圖添加一個蒙版視圖” 。
其中decorView是Activity的一個擴(kuò)展屬性:
val Activity.decorView: FrameLayout?
? ? get() = (takeIf { !isFinishing && !isDestroyed }?.window?.decorView) as? FrameLayout
當(dāng)Activity還展示的時候,從它的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é)累加。
為了避免界面展示出來后黑一下,所以將“添加蒙版”任務(wù)添加到主線程消息隊列的頭部,優(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的封裝,它用更簡潔的語法實現(xiàn)值的存取,且可以忽略類型,
效果如下:
這個方案不是全局的,而是針對單界面的,所以彈出的DialogFragment會在蒙版之上,那就用同樣的方法在對話框上再覆蓋一層蒙版:
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ò)展方法。
最后
一點題外話:
我們有《Android學(xué)習(xí)、面試;文檔、視頻資源免費獲取》,可復(fù)制鏈接后用石墨文檔 App 或小程序打開鏈接或者私信我資料領(lǐng)取。
https://shimo.im/docs/TG8PDh9D96WGTT8W