1.狀態(tài)欄顯示模式介紹
Android系統(tǒng)提供了兩種顯示模式:明亮模式與暗黑模式
明亮模式(Light Model):整體偏亮,即背景亮色,文字等內(nèi)容暗色。
暗黑模式(Dark Model):整體偏暗,即背景暗色,文字等內(nèi)容亮色。
2.Android 狀態(tài)欄的缺陷
Android 狀態(tài)欄內(nèi)容顏色(非背景色)只有黑白兩種,當(dāng)明亮模式時(shí)為黑色主題,當(dāng)暗黑模式時(shí)是白色主題。當(dāng)然顏色只有兩種不算什么缺陷。
當(dāng)狀態(tài)欄背景色改變時(shí),我們需要手動(dòng)設(shè)置顯示模式來改變其內(nèi)容的顏色。這樣極其不方便,尤其是在狀態(tài)欄背景顏色漸變時(shí)。
例如:
微信下拉時(shí),狀態(tài)欄背景色一直在改變,而內(nèi)容顏色沒及時(shí)做改變,如圖紅色框部分。
- 解決方案構(gòu)思
其實(shí)狀態(tài)欄內(nèi)容顏色完全可以自適應(yīng)的,邏輯很簡單,當(dāng)背景暗時(shí)狀態(tài)欄內(nèi)容就亮;當(dāng)背景亮?xí)r狀態(tài)欄內(nèi)容就暗,所以有個(gè)大概思路,當(dāng)每次界面繪制時(shí):
1.獲取狀態(tài)欄像素
2.計(jì)算其平均色值
3.判斷是否為亮色
4.若是亮色則設(shè)置為明亮模式,反之設(shè)置為暗黑模式
4.具體代碼邏輯實(shí)現(xiàn)
/**
* 獲取狀態(tài)欄像素
*/
private fun getStatusBarPixels(activity:Activity) = activity.window.decorView.let {
it.isDrawingCacheEnabled = true
it.buildDrawingCache()
// 截屏
val screenBitmap = it.getDrawingCache()
val width = screenBitmap.width
val height = BarUtils.getStatusBarHeight()
val pixels = IntArray(width * height)
// 獲取狀態(tài)欄區(qū)域像素
screenBitmap.getPixels(pixels, 0, width, 0, 0, width, height)
it.destroyDrawingCache()
pixels
}
/**
* 獲取平均色值
*/
fun getAvgColor(pixels: IntArray): Int {
var r = 0L
var g = 0L
var b = 0L
pixels.forEach {
r += Color.red(it)
g += Color.green(it)
b += Color.blue(it)
}
r /= pixels.size
g /= pixels.size
b /= pixels.size
return Color.rgb(r.toInt(), g.toInt(), b.toInt())
}
以上代碼是自動(dòng)獲取手機(jī)狀態(tài)欄的色值并計(jì)算出平均值
那么下面的方法就可以直接傳入色值來動(dòng)態(tài)改變狀態(tài)欄的主題顏色了
/**
* 是否為亮色
* 可以單獨(dú)傳入 色值 判斷是否為亮色
*/
fun isLightColor(@ColorInt color: Int) =
(computeLuminance(color) + 0.05).pow(2.0) > 0.15
/**
* 是否為亮色
* 自動(dòng)獲取狀態(tài)欄的色紙 判斷是否為亮色
*/
fun isLightColor(activity:Activity):Boolean{
val pixels = getStatusBarPixels(activity)
val color = getAvgColor(pixels)
val computeColor = computeLuminance(color) + 0.05
return (computeColor.pow(2.0) > 0.15)
}
/**
* 顏色亮度
*/
private fun computeLuminance(@ColorInt color: Int) =
0.2126 * linearizeColorComponent(Color.red(color)) +
0.7152 * linearizeColorComponent(Color.green(color)) +
0.0722 * linearizeColorComponent(Color.blue(color))
/**
* 線性化顏色分量
*/
private fun linearizeColorComponent(colorComponent: Int) = (colorComponent / 255.0).let {
if (it <= 0.03928) it / 12.92 else ((it + 0.055) / 1.055).pow(2.4)
}
5.運(yùn)用中性能優(yōu)化
/**
* 性能優(yōu)化:單線程池,更新阻塞時(shí)只做最后一次更新
*/
private val executor by lazy {
object : ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, ArrayBlockingQueue(1)) {
override fun execute(command: Runnable?) {
queue.clear()
super.execute(command)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
BarUtils.transparentStatusBar(this) // 需要沉浸狀態(tài)欄,才能截屏至狀態(tài)欄
window.decorView.viewTreeObserver.addOnDrawListener(this)
}
override fun onDestroy() {
window.decorView.viewTreeObserver.removeOnDrawListener(this)
super.onDestroy()
}
override fun onDraw() {
executor.execute {
try {
// 獲取狀態(tài)欄像素
val pixels = getStatusBarPixels()
// 計(jì)算平均色值
val avgColor = getAvgColor(pixels)
// 判斷是否為亮色
val isLight = isLightColor(avgColor)
runOnUiThread {
// 設(shè)置 LightModel
if (!isDestroyed) BarUtils.setStatusBarLightMode(this, isLight)
}
} catch (_: Exception) {
}
}
}
6.整體代碼使用示例
class MainActivity : AppCompatActivity(), ViewTreeObserver.OnDrawListener {
/**
* 性能優(yōu)化:單線程池,更新阻塞時(shí)只做最后一次更新
*/
private val executor by lazy {
object : ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, ArrayBlockingQueue(1)) {
override fun execute(command: Runnable?) {
queue.clear()
super.execute(command)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
BarUtils.transparentStatusBar(this) // 需要沉浸狀態(tài)欄,才能截屏至狀態(tài)欄
window.decorView.viewTreeObserver.addOnDrawListener(this)
}
override fun onDestroy() {
window.decorView.viewTreeObserver.removeOnDrawListener(this)
super.onDestroy()
}
override fun onDraw() {
executor.execute {
try {
// 獲取狀態(tài)欄像素
val pixels = getStatusBarPixels()
// 計(jì)算平均色值
val avgColor = getAvgColor(pixels)
// 判斷是否為亮色
val isLight = isLightColor(avgColor)
runOnUiThread {
// 設(shè)置 LightModel
if (!isDestroyed) BarUtils.setStatusBarLightMode(this, isLight)
}
} catch (_: Exception) {
}
}
}
/**
* 獲取狀態(tài)欄像素
*/
private fun getStatusBarPixels() = window.decorView.let {
it.isDrawingCacheEnabled = true
it.buildDrawingCache()
// 截屏
val screenBitmap = it.getDrawingCache()
val width = screenBitmap.width
val height = BarUtils.getStatusBarHeight()
val pixels = IntArray(width * height)
// 獲取狀態(tài)欄區(qū)域像素
screenBitmap.getPixels(pixels, 0, width, 0, 0, width, height)
it.destroyDrawingCache()
pixels
}
/**
* 獲取平均色值
*/
private fun getAvgColor(pixels: IntArray): Int {
var r = 0L
var g = 0L
var b = 0L
pixels.forEach {
r += Color.red(it)
g += Color.green(it)
b += Color.blue(it)
}
r /= pixels.size
g /= pixels.size
b /= pixels.size
return Color.rgb(r.toInt(), g.toInt(), b.toInt())
}
/**
* 是否為亮色
* 可以單獨(dú)傳入 色值 判斷是否為亮色
*/
fun isLightColor(@ColorInt color: Int) =
(computeLuminance(color) + 0.05).pow(2.0) > 0.15
/**
* 是否為亮色
* 自動(dòng)獲取狀態(tài)欄的色紙 判斷是否為亮色
*/
fun isLightColor(activity:Activity):Boolean{
val pixels = getStatusBarPixels(activity)
val color = getAvgColor(pixels)
val computeColor = computeLuminance(color) + 0.05
return (computeColor.pow(2.0) > 0.15)
}
/**
* 顏色亮度
*/
private fun computeLuminance(@ColorInt color: Int) =
0.2126 * linearizeColorComponent(Color.red(color)) +
0.7152 * linearizeColorComponent(Color.green(color)) +
0.0722 * linearizeColorComponent(Color.blue(color))
/**
* 線性化顏色分量
*/
private fun linearizeColorComponent(colorComponent: Int) = (colorComponent / 255.0).let {
if (it <= 0.03928) it / 12.92 else ((it + 0.055) / 1.055).pow(2.4)
}
}
以上就是狀態(tài)欄的主題跟隨狀態(tài)欄背景色自動(dòng)設(shè)置變化,也可以自己傳入色值來判斷是否是亮色而設(shè)置狀態(tài)欄的主題。