分割线改fontMetrics直接计算修复消失;删除线按墨迹中点自绘居中;置顶圆圈粗细与底部对齐调整,版本 1.8

This commit is contained in:
XIGE
2026-11-30 00:10:10 +08:00
parent 035da90a76
commit 3cad26fbdb
5 changed files with 86 additions and 41 deletions

View File

@@ -145,19 +145,19 @@ object LockOverlay {
}
/**
* 横屏排版里的分割线只分割两块大时间:高度对齐倒计时文字的实际渲染高度。
* 横屏排版里的分割线只分割两块大时间:高度直接由 paint.fontMetrics 算出
* (includeFontPadding=false 时 TextView 高度就是 ascent..descent 跨度),
* 不依赖布局完成时机——上一版 View.post 在悬浮窗链路上时序不稳导致线消失。
* 竖屏布局没有这个控件,查找结果为 null 直接跳过。
*/
private fun syncDivider(view: View) {
val divider = view.findViewById<View>(R.id.lockDivider) ?: return
val countdown = view.findViewById<TextView>(R.id.tvLockCountdown) ?: return
divider.post {
if (countdown.height > 0 && divider.layoutParams != null &&
divider.layoutParams.height != countdown.height
) {
divider.layoutParams.height = countdown.height
divider.requestLayout()
}
val fm = countdown.paint.fontMetrics
val h = (fm.descent - fm.ascent).toInt()
if (h > 0 && divider.layoutParams != null && divider.layoutParams.height != h) {
divider.layoutParams.height = h
divider.requestLayout()
}
}

View File

@@ -163,18 +163,19 @@ class LockScreenActivity : AppCompatActivity() {
}
/**
* 横屏排版里的分割线只分割两块大时间:高度对齐倒计时文字的实际渲染高度,
* 不再盖住下方的任务名/日期行。竖屏布局没有这个控件,查找结果为 null 直接跳过。
* 横屏排版里的分割线只分割两块大时间,高度对齐倒计时文字的实际渲染高度。
* 上一版用 View.post 等布局完成后再取 height,实测在悬浮窗/锁屏链路上
* 时机不稳导致分割线整条消失;这里改为直接由 paint.fontMetrics 算出
* (includeFontPadding=false 时 TextView 高度就是 ascent..descent 跨度),
* 零时序依赖、立即生效。竖屏布局没有这个控件,查找结果为 null 直接跳过。
*/
private fun syncDivider() {
val divider = findViewById<View>(R.id.lockDivider) ?: return
divider.post {
if (tvCountdown.height > 0 && divider.layoutParams != null &&
divider.layoutParams.height != tvCountdown.height
) {
divider.layoutParams.height = tvCountdown.height
divider.requestLayout()
}
val fm = tvCountdown.paint.fontMetrics
val h = (fm.descent - fm.ascent).toInt()
if (h > 0 && divider.layoutParams != null && divider.layoutParams.height != h) {
divider.layoutParams.height = h
divider.requestLayout()
}
}

View File

@@ -539,6 +539,6 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
}
companion object {
const val VERSION = "1.7"
const val VERSION = "1.8"
}
}

View File

@@ -127,14 +127,12 @@ class TaskAdapter(
)
}
// 完成横线只画任务名本身(StrikethroughSpan 限定区间),
// 后面的重要度/紧急度/置顶图标不跟着划线。
// 完成横线只画任务名本身(MidStrikeSpan 限定区间,且划线取文字墨迹
// 的垂直中点——系统 StrikethroughSpan 用整字面半高定位,数字/汉字
// 会让线显得偏下)。重要度/紧急度/置顶图标不跟着划线。
// 注意:span 必须在 tvTitle.text = title 之前挂好
if (task.done) {
title.setSpan(
android.text.style.StrikethroughSpan(),
0, task.title.length, 0,
)
title.setSpan(MidStrikeSpan(), 0, task.title.length, 0)
}
tvTitle.text = title
tvTitle.paintFlags = tvTitle.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
@@ -208,6 +206,36 @@ class TaskAdapter(
}
}
/**
* 任务名删除线:系统 StrikethroughSpan 的划线位置按"字体半高"(baseline + ascent/2)
* 计算,但数字/汉字的实际墨迹顶比字体 ascent 低不少,划线视觉上偏下。
* 这里自绘:按实际墨迹边界(getTextBounds)取垂直中点,线严格穿过文字中间。
*/
private class MidStrikeSpan : android.text.style.ReplacementSpan() {
override fun getSize(
paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?,
): Int = paint.measureText(text, start, end).toInt()
override fun draw(
canvas: android.graphics.Canvas, text: CharSequence, start: Int, end: Int,
x: Float, top: Int, baseline: Int, bottom: Int, paint: Paint,
) {
// 先按原样式画字(无删除线标志)
canvas.drawText(text, start, end, x, baseline.toFloat(), paint)
val s = text.toString()
val bounds = android.graphics.Rect()
paint.getTextBounds(s, start, end, bounds)
// 墨迹垂直中点:bounds.top/bottom 相对基线的偏移(top 为负)
val lineY = baseline + (bounds.top + bounds.bottom) / 2f
val w = paint.measureText(text, start, end)
val line = Paint(Paint.ANTI_ALIAS_FLAG)
line.color = paint.color
line.strokeWidth = (paint.textSize / 28f).coerceAtLeast(1f)
canvas.drawLine(x, lineY, x + w, lineY, line)
}
}
/**
* 置顶图标:圆圈内向上箭头。
* 自绘而不是用字体字形——常用字体里没有"圆圈+箭头"的组合字符,
@@ -238,24 +266,29 @@ private class PinIconSpan(
x: Float, top: Int, baseline: Int, bottom: Int, paint: Paint,
) {
val s = size
// 与 ①②③ 底部对齐:字体里的圆圈数字底缘略微越过基线(约 -0.05 个图标高),
// 圆心取基线上方 0.45 个图标高,比之前 0.50 略下移,底部不再"悬空"显偏上
val cx = x + s / 2f
// 与 ①②③ 字形对齐:圆圈数字贴基线站立,圆心约在基线上方 0.5 个图标高
val cy = baseline - s * 0.5f
// 不能用 apply { this.color = ... }:作用域里 color 会命中 Paint 自己的
// color 属性(默认黑色),把外面的品牌蓝遮掉——就是那版"图标变黑"的原因
val p = Paint(Paint.ANTI_ALIAS_FLAG)
p.color = iconColor
p.style = Paint.Style.STROKE
p.strokeWidth = s / 8f
p.strokeCap = Paint.Cap.ROUND
canvas.drawCircle(cx, cy, s / 2f - p.strokeWidth / 2f, p)
val cy = baseline - s * 0.45f
// 圈线与重要度圆圈数字的描边粗细一致(约 1/10 图标高)
val ring = Paint(Paint.ANTI_ALIAS_FLAG)
ring.color = iconColor
ring.style = Paint.Style.STROKE
ring.strokeWidth = s / 10f
ring.strokeCap = Paint.Cap.ROUND
canvas.drawCircle(cx, cy, s / 2f - ring.strokeWidth / 2f, ring)
// 向上箭头:竖杆 + 两条斜线,整体落在圆内
val head = s * 0.17f
val yTop = cy - s * 0.15f
val yBot = cy + s * 0.19f
canvas.drawLine(cx, yBot, cx, yTop, p)
canvas.drawLine(cx - head, yTop + head, cx, yTop, p)
canvas.drawLine(cx + head, yTop + head, cx, yTop, p)
// 箭头比圈线更细(约 1/15 图标高),整体落在圆内
val arrow = Paint(Paint.ANTI_ALIAS_FLAG)
arrow.color = iconColor
arrow.style = Paint.Style.STROKE
arrow.strokeWidth = s / 15f
arrow.strokeCap = Paint.Cap.ROUND
val head = s * 0.18f
val yTop = cy - s * 0.16f
val yBot = cy + s * 0.18f
canvas.drawLine(cx, yBot, cx, yTop, arrow)
canvas.drawLine(cx - head, yTop + head, cx, yTop, arrow)
canvas.drawLine(cx + head, yTop + head, cx, yTop, arrow)
}
}