267 lines
10 KiB
Kotlin
267 lines
10 KiB
Kotlin
package com.focusbox.app.widget
|
||
|
||
import android.content.Context
|
||
import android.graphics.Canvas
|
||
import android.graphics.Color
|
||
import android.graphics.Paint
|
||
import android.graphics.RectF
|
||
import android.text.TextPaint
|
||
import android.text.TextUtils
|
||
import android.util.AttributeSet
|
||
import android.view.View
|
||
import com.focusbox.app.R
|
||
import com.focusbox.app.util.Dates
|
||
import com.focusbox.app.util.col
|
||
import com.focusbox.app.util.dpf
|
||
import com.focusbox.app.util.spf
|
||
import kotlin.math.cos
|
||
import kotlin.math.sin
|
||
|
||
/**
|
||
* 带引线标注的环形图。
|
||
*
|
||
* 与普通占比环的区别:项目名与时长不放在图外的列表里,而是用折线牵到环的两侧
|
||
* (左侧标注右对齐、右侧标注左对齐,圆点统一落在外侧边缘),这样一眼就能把
|
||
* 色块和"这是谁、花了多久"对上,不用在图例里来回找。
|
||
*
|
||
* 标注位置要自己算:同一侧的标签按角度落点排序后做最小间距夹逼,
|
||
* 否则相邻扇区的两行字会叠在一起。
|
||
*/
|
||
class LabelDonutView @JvmOverloads constructor(
|
||
context: Context,
|
||
attrs: AttributeSet? = null,
|
||
) : View(context, attrs) {
|
||
|
||
data class Slice(val name: String, val seconds: Int, val color: Int)
|
||
|
||
private var slices: List<Slice> = emptyList()
|
||
|
||
/** 环中心的大字(总时长) */
|
||
var centerText: String = ""
|
||
set(value) {
|
||
field = value
|
||
invalidate()
|
||
}
|
||
|
||
/** 环中心的小字(总专注 / 任务占比) */
|
||
var centerCaption: String = ""
|
||
set(value) {
|
||
field = value
|
||
invalidate()
|
||
}
|
||
|
||
/** 最多画几条引线标注,多出来的合并成「其他」,避免标注挤成一团 */
|
||
var maxLabels: Int = 6
|
||
|
||
private val strokeW = context.dpf(22f)
|
||
|
||
private val arcPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||
style = Paint.Style.STROKE
|
||
strokeWidth = strokeW
|
||
strokeCap = Paint.Cap.BUTT
|
||
}
|
||
private val trackPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||
style = Paint.Style.STROKE
|
||
strokeWidth = strokeW
|
||
color = context.col(R.color.ring_track)
|
||
}
|
||
private val pctPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||
textSize = context.spf(11f)
|
||
textAlign = Paint.Align.CENTER
|
||
isFakeBoldText = true
|
||
}
|
||
private val centerPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||
textSize = context.spf(19f)
|
||
color = context.col(R.color.title)
|
||
textAlign = Paint.Align.CENTER
|
||
isFakeBoldText = true
|
||
}
|
||
private val captionPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||
textSize = context.spf(10f)
|
||
color = context.col(R.color.text_tertiary)
|
||
textAlign = Paint.Align.CENTER
|
||
}
|
||
private val linePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||
style = Paint.Style.STROKE
|
||
strokeWidth = context.dpf(1.2f)
|
||
}
|
||
private val dotPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL }
|
||
private val tagValuePaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
|
||
textSize = context.spf(15f)
|
||
isFakeBoldText = true
|
||
}
|
||
private val tagNamePaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
|
||
textSize = context.spf(11f)
|
||
}
|
||
|
||
fun setSlices(list: List<Slice>) {
|
||
slices = list
|
||
invalidate()
|
||
}
|
||
|
||
private data class Tag(
|
||
val slice: Slice,
|
||
val midDeg: Float,
|
||
val side: Int,
|
||
val anchorY: Float,
|
||
var y: Float = anchorY,
|
||
)
|
||
|
||
override fun onDraw(canvas: Canvas) {
|
||
val total = slices.sumOf { it.seconds }
|
||
val cx = width / 2f
|
||
val cy = height / 2f
|
||
|
||
// 环的直径同时受宽高约束:宽度让出两侧标注区,高度不足时优先保证标注能排开
|
||
val outer = minOf(width * 0.48f, height * 0.62f)
|
||
val r = (outer / 2f - strokeW / 2f).coerceAtLeast(context.dpf(24f))
|
||
|
||
canvas.drawCircle(cx, cy, r, trackPaint)
|
||
|
||
if (total <= 0) {
|
||
drawCenter(canvas, cx, cy, context.getString(R.string.no_record))
|
||
return
|
||
}
|
||
|
||
val shown = collapse(slices)
|
||
val box = RectF(cx - r, cy - r, cx + r, cy + r)
|
||
val gapDeg = if (shown.size > 1) 2.2f else 0f
|
||
|
||
// ---- 弧与百分比
|
||
var start = -90f
|
||
val tags = ArrayList<Tag>(shown.size)
|
||
shown.forEach { s ->
|
||
val sweep = s.seconds.toFloat() / total * 360f
|
||
val midDeg = start + sweep / 2f
|
||
arcPaint.color = s.color
|
||
canvas.drawArc(box, start + gapDeg / 2f, (sweep - gapDeg).coerceAtLeast(0.5f), false, arcPaint)
|
||
|
||
val arcLen = sweep / 360f * (2 * Math.PI.toFloat() * r)
|
||
val pct = "${(s.seconds * 100f / total).toInt().coerceAtLeast(1)}%"
|
||
// 环加粗后百分比必须完整落在色带里:按实测字宽缩字号,左右各留 3dp 边距
|
||
var pctSize = 11f
|
||
pctPaint.textSize = context.spf(pctSize)
|
||
while (pctPaint.measureText(pct) > arcLen - context.dpf(6f) && pctSize > 8f) {
|
||
pctSize -= 0.5f
|
||
pctPaint.textSize = context.spf(pctSize)
|
||
}
|
||
if (arcLen >= pctPaint.measureText(pct) + context.dpf(4f)) {
|
||
val rad = Math.toRadians(midDeg.toDouble())
|
||
val px = cx + r * cos(rad).toFloat()
|
||
val py = cy + r * sin(rad).toFloat()
|
||
pctPaint.color = readableOn(s.color)
|
||
val fm = pctPaint.fontMetrics
|
||
canvas.drawText(pct, px, py - (fm.ascent + fm.descent) / 2f, pctPaint)
|
||
}
|
||
|
||
// 太薄的扇区不牵线,否则几条线会叠在同一个点上
|
||
if (sweep >= 5f) {
|
||
val cosA = cos(radians(midDeg))
|
||
val sinA = sin(radians(midDeg))
|
||
tags.add(
|
||
Tag(
|
||
slice = s,
|
||
midDeg = midDeg,
|
||
side = if (cosA >= 0.0) 1 else -1,
|
||
anchorY = cy + ((r + strokeW) * sinA).toFloat(),
|
||
)
|
||
)
|
||
}
|
||
start += sweep
|
||
}
|
||
|
||
drawCenter(canvas, cx, cy, null)
|
||
layoutTags(tags)
|
||
tags.forEach { drawTag(canvas, it, cx, cy, r) }
|
||
}
|
||
|
||
private fun radians(deg: Float): Double = Math.toRadians(deg.toDouble())
|
||
|
||
private fun collapse(list: List<Slice>): List<Slice> {
|
||
if (list.size <= maxLabels) return list
|
||
val head = list.take(maxLabels - 1)
|
||
val rest = list.drop(maxLabels - 1)
|
||
return head + Slice(
|
||
name = context.getString(R.string.slice_other),
|
||
seconds = rest.sumOf { it.seconds },
|
||
color = context.col(R.color.text_tertiary),
|
||
)
|
||
}
|
||
|
||
private fun drawCenter(canvas: Canvas, cx: Float, cy: Float, emptyHint: String?) {
|
||
val big = centerText.takeIf { it.isNotBlank() } ?: "0m"
|
||
canvas.drawText(big, cx, cy + context.dpf(2f), centerPaint)
|
||
val cap = emptyHint ?: centerCaption
|
||
if (cap.isNotBlank()) {
|
||
canvas.drawText(cap, cx, cy + context.dpf(18f), captionPaint)
|
||
}
|
||
}
|
||
|
||
/** 同侧标签按落点排序后做最小间距夹逼,再整体收回可视区 */
|
||
private fun layoutTags(tags: List<Tag>) {
|
||
val minGap = context.dpf(29f)
|
||
// 上下留出两行字的高度:时间在 y 上方、名称在 y 下方,压到边界会被裁掉
|
||
val top = context.dpf(15f)
|
||
val bottom = height - context.dpf(17f)
|
||
for (side in intArrayOf(-1, 1)) {
|
||
val group = tags.filter { it.side == side }.sortedBy { it.anchorY }
|
||
if (group.isEmpty()) continue
|
||
group.forEachIndexed { i, t ->
|
||
t.y = if (i == 0) maxOf(t.anchorY, top) else maxOf(t.anchorY, group[i - 1].y + minGap)
|
||
}
|
||
val overflow = group.last().y - bottom
|
||
if (overflow > 0f) group.forEach { it.y -= overflow }
|
||
// 极端情况下(标注多、控件矮)再压回上界,宁可间距略小也不出画布
|
||
val lo = minOf(top, bottom)
|
||
val hi = maxOf(top, bottom)
|
||
group.forEach { it.y = it.y.coerceIn(lo, hi) }
|
||
}
|
||
}
|
||
|
||
private fun drawTag(canvas: Canvas, tag: Tag, cx: Float, cy: Float, r: Float) {
|
||
val side = tag.side
|
||
val color = tag.slice.color
|
||
val dotR = context.dpf(2.6f)
|
||
val dotX = if (side > 0) width - context.dpf(3f) - dotR else context.dpf(3f) + dotR
|
||
// 折点落在环外缘再往外 10dp,环加粗后引线不会压在色带上
|
||
val kneeX = cx + side * (r + strokeW / 2f + context.dpf(10f))
|
||
|
||
val cosA = cos(radians(tag.midDeg)).toFloat()
|
||
val sinA = sin(radians(tag.midDeg)).toFloat()
|
||
val startX = cx + (r + strokeW / 2f) * cosA
|
||
val startY = cy + (r + strokeW / 2f) * sinA
|
||
|
||
linePaint.color = color
|
||
linePaint.alpha = 200
|
||
// 从环外缘斜出到折点,再水平指向外侧圆点
|
||
canvas.drawLine(startX, startY, kneeX, tag.y, linePaint)
|
||
canvas.drawLine(kneeX, tag.y, dotX, tag.y, linePaint)
|
||
|
||
dotPaint.color = color
|
||
canvas.drawCircle(dotX, tag.y, dotR, dotPaint)
|
||
|
||
val textX = dotX - side * context.dpf(9f)
|
||
val maxW = (kotlin.math.abs(textX - (cx + side * (r + strokeW / 2f))) - context.dpf(8f))
|
||
.coerceAtLeast(context.dpf(20f))
|
||
|
||
tagValuePaint.color = color
|
||
tagValuePaint.textAlign = if (side > 0) Paint.Align.RIGHT else Paint.Align.LEFT
|
||
tagNamePaint.color = color
|
||
tagNamePaint.textAlign = tagValuePaint.textAlign
|
||
|
||
val value = Dates.durationBrief(tag.slice.seconds)
|
||
val name = TextUtils.ellipsize(
|
||
tag.slice.name, tagNamePaint, maxW, TextUtils.TruncateAt.END,
|
||
).toString()
|
||
|
||
canvas.drawText(value, textX, tag.y - context.dpf(3f), tagValuePaint)
|
||
canvas.drawText(name, textX, tag.y + context.dpf(13f), tagNamePaint)
|
||
}
|
||
|
||
/** 环上的百分比文字:底色偏亮时改黑字,否则看不出来 */
|
||
private fun readableOn(back: Int): Int {
|
||
val lum = 0.299f * Color.red(back) + 0.587f * Color.green(back) + 0.114f * Color.blue(back)
|
||
return if (lum > 170f) Color.argb(210, 0, 0, 0) else Color.argb(235, 255, 255, 255)
|
||
}
|
||
}
|