饼图标签间距改按字体度量计算,随系统字号自适应,杜绝周月年多切片重叠(v0.15)

This commit is contained in:
XIGE
2026-11-29 11:49:02 +08:00
parent f205eda064
commit 27a47892e9
3 changed files with 31 additions and 22 deletions

View File

@@ -1,5 +1,9 @@
# 变更记录 # 变更记录
## 2026-11-29 · 饼图标签间距改按字体度量计算,随系统字号自适应,杜绝周月年多切片重叠(v0.15)
- 同步源码更新 2 个文件(新增/修改 2,删除 0)。
## 2026-11-29 · 修复饼图标注重叠:周月年切片多时间距自动压缩,不再文字数字重合(v0.14) ## 2026-11-29 · 修复饼图标注重叠:周月年切片多时间距自动压缩,不再文字数字重合(v0.14)
- LabelDonutView.layoutTags:一侧标签理想间距 29dp 放不下时按可用高度自动压缩(间距 = 可用高度 / 间隔数),替代原先"溢出整体上移 + coerceIn 钳回边界"把多个标签压到同一 y 的旧逻辑,相邻标注保证不重叠;极端小控件场景改为在可视区内均匀铺开 - LabelDonutView.layoutTags:一侧标签理想间距 29dp 放不下时按可用高度自动压缩(间距 = 可用高度 / 间隔数),替代原先"溢出整体上移 + coerceIn 钳回边界"把多个标签压到同一 y 的旧逻辑,相邻标注保证不重叠;极端小控件场景改为在可视区内均匀铺开

View File

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

View File

@@ -197,40 +197,45 @@ class LabelDonutView @JvmOverloads constructor(
} }
} }
/** 同侧标签按落点排序后做最小间距夹逼,再整体收回可视区 */ /**
* 同侧标签按落点排序后做最小间距夹逼。
*
* 最小间距不能写死:标签占位 = 间隙 + 时间行高 + 间隙 + 任务名行高,
* 随系统字号/字体(鸿蒙 Sans 字面更高)缩放,固定 29dp 在大字号下
* 会让相邻标签的时间压到上一个标签的任务名上。
* 这里全部由字体度量实时算出,放不下时压缩间距,任何字号都不重叠。
*/
private fun layoutTags(tags: List<Tag>) { private fun layoutTags(tags: List<Tag>) {
val top = context.dpf(15f) val gap = context.dpf(4f)
val bottom = height - context.dpf(17f) val vfm = tagValuePaint.fontMetrics
val nfm = tagNamePaint.fontMetrics
// 时间在引线上方占 (gap + valueCell),任务名在下方占 (gap + nameCell)
val valueCell = -vfm.ascent
val nameCell = -nfm.ascent + nfm.descent
val ideal = gap + valueCell + gap + nameCell + context.dpf(2f)
// 上下边界同样按度量算:最顶标签的时间、最底标签的任务名不被裁切
val top = gap + valueCell + context.dpf(2f)
val bottom = height - (gap + nameCell + context.dpf(2f))
val avail = (bottom - top).coerceAtLeast(0f) val avail = (bottom - top).coerceAtLeast(0f)
val ideal = context.dpf(29f)
for (side in intArrayOf(-1, 1)) { for (side in intArrayOf(-1, 1)) {
val group = tags.filter { it.side == side }.sortedBy { it.anchorY } val group = tags.filter { it.side == side }.sortedBy { it.anchorY }
if (group.isEmpty()) continue if (group.isEmpty()) continue
// 理想间距 29dp;周/月/年切片多时一侧放不下,
// 按可用高度压缩间距(间距 = 可用高度 / 间隔数),
// 保证相邻标签永不重叠——原先"溢出整体上移+钳回边界"
// 会把多个标签压到同一 y,文字数字叠在一起。
val minGap = if (group.size > 1) { val minGap = if (group.size > 1) {
minOf(ideal, avail / (group.size - 1)) minOf(ideal, avail / (group.size - 1))
} else ideal } else ideal
// 前向:从上往下推,保证与上一个标签的间距
group.forEachIndexed { i, t -> group.forEachIndexed { i, t ->
t.y = if (i == 0) maxOf(t.anchorY, top) else maxOf(t.anchorY, group[i - 1].y + minGap) t.y = if (i == 0) maxOf(t.anchorY, top) else maxOf(t.anchorY, group[i - 1].y + minGap)
} }
// 少量溢出(间距未压缩仍超界):整体上移,首端不低于 top // 末端超出底部:从下往上收(每个标签不超过下一个的 y - 间距,也不出顶界),
val overflow = group.last().y - bottom // 不会把标签压到同一位置
if (overflow > 0f) { if (group.last().y > bottom) {
group.forEach { it.y -= overflow } group[group.size - 1].y = bottom
if (group.first().y < top) { for (i in group.size - 2 downTo 0) {
// 上下都顶住仍放不下(极端小控件):在 [top, bottom] 内均匀铺开 group[i].y = minOf(group[i].y, group[i + 1].y - minGap).coerceAtLeast(top)
val n = group.size
if (n == 1) {
group[0].y = top
} else {
val step = avail / (n - 1)
group.forEachIndexed { i, t -> t.y = top + i * step }
}
} }
} }
} }