style: 电池显示优化

- 百分比文字放大 15f→18f,颜色跟随电池状态
- 充电时加  前缀(如 73%),一眼可见充电状态
- 颜色分级更清晰:>60%绿 / 21-60%白 / 11-20%橙 / ≤10%红
- 低电量时电池壳也变红,整体醒目
- 统一 getBatteryColor() 逻辑复用

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dongliang
2026-05-07 13:35:27 +09:30
parent 6f11784b8a
commit 9eb618dfd6

View File

@@ -79,13 +79,23 @@ class StatusBarView @JvmOverloads constructor(
} }
} }
/** 获取电池状态颜色(统一逻辑) */
private fun getBatteryColor(): Int = when {
isCharging -> colorGreen
batteryLevel <= 10 -> colorRed
batteryLevel <= 20 -> colorOrange
batteryLevel <= 60 -> colorWhite
else -> colorGreen
}
/** 绘制电量百分比文字(电池图标左侧) */ /** 绘制电量百分比文字(电池图标左侧) */
private fun drawBatteryText(canvas: Canvas, rightEdge: Float, centerY: Float) { private fun drawBatteryText(canvas: Canvas, rightEdge: Float, centerY: Float) {
if (batteryLevel < 0) return // 未获取时不显示 if (batteryLevel < 0) return // 未获取时不显示
val text = "${batteryLevel}%" // 充电时显示 ⚡ 前缀
val text = if (isCharging) "${batteryLevel}%" else "${batteryLevel}%"
paint.style = Paint.Style.FILL paint.style = Paint.Style.FILL
paint.color = 0x99FFFFFF.toInt() // 白色 60% 透明度 paint.color = getBatteryColor()
paint.textSize = 15f paint.textSize = 18f
paint.textAlign = Paint.Align.RIGHT paint.textAlign = Paint.Align.RIGHT
val metrics = paint.fontMetrics val metrics = paint.fontMetrics
val baselineY = centerY - (metrics.ascent + metrics.descent) / 2 val baselineY = centerY - (metrics.ascent + metrics.descent) / 2
@@ -93,28 +103,23 @@ class StatusBarView @JvmOverloads constructor(
paint.textAlign = Paint.Align.LEFT paint.textAlign = Paint.Align.LEFT
} }
/** 绘制电池图标(壳 + 填充条 + 凸起 + 充电闪电 */ /** 绘制电池图标(壳 + 填充条 + 凸起) */
private fun drawBattery(canvas: Canvas, startX: Float, centerY: Float) { private fun drawBattery(canvas: Canvas, startX: Float, centerY: Float) {
val shellW = 28f val shellW = 28f
val shellH = 13f val shellH = 13f
val shellTop = centerY - shellH / 2 val shellTop = centerY - shellH / 2
val cornerR = 3.5f val cornerR = 3.5f
// 电池壳边框 // 电池壳边框(颜色跟随状态)
paint.style = Paint.Style.STROKE paint.style = Paint.Style.STROKE
paint.strokeWidth = 1.5f paint.strokeWidth = 1.5f
paint.color = colorBorder paint.color = if (batteryLevel <= 10 && !isCharging) colorRed else colorBorder
val shellRect = RectF(startX, shellTop, startX + shellW, shellTop + shellH) val shellRect = RectF(startX, shellTop, startX + shellW, shellTop + shellH)
canvas.drawRoundRect(shellRect, cornerR, cornerR, paint) canvas.drawRoundRect(shellRect, cornerR, cornerR, paint)
// 电池填充条 // 电池填充条
paint.style = Paint.Style.FILL paint.style = Paint.Style.FILL
paint.color = when { paint.color = getBatteryColor()
isCharging -> colorGreen
batteryLevel <= 10 -> colorRed
batteryLevel <= 20 -> colorOrange
else -> colorGreen
}
val fillPadding = 2.5f val fillPadding = 2.5f
val fillMaxW = shellW - fillPadding * 2 val fillMaxW = shellW - fillPadding * 2
val fillW = fillMaxW * batteryLevel.coerceAtLeast(0) / 100f val fillW = fillMaxW * batteryLevel.coerceAtLeast(0) / 100f
@@ -126,10 +131,8 @@ class StatusBarView @JvmOverloads constructor(
) )
canvas.drawRoundRect(fillRect, 2f, 2f, paint) canvas.drawRoundRect(fillRect, 2f, 2f, paint)
// 充电状态通过填充条颜色区分(绿色=充电中,已在上方 when 处理)
// 电池凸起(右侧小矩形) // 电池凸起(右侧小矩形)
paint.color = colorBorder paint.color = if (batteryLevel <= 10 && !isCharging) colorRed else colorBorder
val nubW = 2.5f val nubW = 2.5f
val nubH = 6f val nubH = 6f
val nubRect = RectF( val nubRect = RectF(