分割线改fontMetrics直接计算修复消失;删除线按墨迹中点自绘居中;置顶圆圈粗细与底部对齐调整,版本 1.8
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user