-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathDebugOverlayDrawable.kt
More file actions
103 lines (86 loc) · 2.81 KB
/
DebugOverlayDrawable.kt
File metadata and controls
103 lines (86 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package io.sentry.android.replay.util
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.drawable.Drawable
internal class DebugOverlayDrawable : Drawable() {
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val padding = 6f
private val tmpRect = Rect()
private var masks: List<Rect> = emptyList()
companion object {
private val maskBackgroundColor = Color.argb(32, 255, 20, 20)
private val maskBorderColor = Color.argb(128, 255, 20, 20)
private const val TEXT_COLOR = Color.BLACK
private const val TEXT_OUTLINE_COLOR = Color.WHITE
private const val STROKE_WIDTH = 6f
private const val TEXT_SIZE = 32f
}
override fun draw(canvas: Canvas) {
paint.textSize = TEXT_SIZE
paint.setColor(Color.BLACK)
paint.strokeWidth = STROKE_WIDTH
for (mask in masks) {
paint.setColor(maskBackgroundColor)
paint.style = Paint.Style.FILL
canvas.drawRect(mask, paint)
paint.setColor(maskBorderColor)
paint.style = Paint.Style.STROKE
canvas.drawRect(mask, paint)
val topLeftLabel = "${mask.left}/${mask.top}"
paint.getTextBounds(topLeftLabel, 0, topLeftLabel.length, tmpRect)
drawTextWithOutline(
canvas,
topLeftLabel,
mask.left.toFloat(),
mask.top.toFloat()
)
val bottomRightLabel = "${mask.right}/${mask.bottom}"
paint.getTextBounds(bottomRightLabel, 0, bottomRightLabel.length, tmpRect)
drawTextWithOutline(
canvas,
bottomRightLabel,
mask.right.toFloat() - tmpRect.width(),
mask.bottom.toFloat() + tmpRect.height()
)
}
}
private fun drawTextWithOutline(
canvas: Canvas,
bottomRightLabel: String,
x: Float,
y: Float
) {
paint.setColor(TEXT_OUTLINE_COLOR)
paint.style = Paint.Style.STROKE
canvas.drawText(
bottomRightLabel,
x,
y,
paint
)
paint.setColor(TEXT_COLOR)
paint.style = Paint.Style.FILL
canvas.drawText(
bottomRightLabel,
x,
y,
paint
)
}
override fun setAlpha(alpha: Int) {
// no-op
}
override fun setColorFilter(colorFilter: ColorFilter?) {
// no-op
}
@Deprecated("Deprecated in Java")
override fun getOpacity(): Int = PixelFormat.TRANSLUCENT
fun updateMasks(masks: List<Rect>) {
this.masks = masks
invalidateSelf()
}
}