Skip to content

Instantly share code, notes, and snippets.

@ntlv
Created March 20, 2021 16:41
Show Gist options
  • Save ntlv/6cd30f6fcdbd13a953ff8127f9f9d528 to your computer and use it in GitHub Desktop.
Save ntlv/6cd30f6fcdbd13a953ff8127f9f9d528 to your computer and use it in GitHub Desktop.
debug view that draws a cross hair in its middle and draws another cross hair at a designated location
class DebugView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
var debugPoint = PointF(0f, 0f)
set(value) {
field = value
invalidate()
}
private val paint = Paint().apply {
color = Color.RED
strokeWidth = 5f
textSize = 50f
}
private val paint2 = Paint(paint).apply {
color = Color.MAGENTA
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.drawColor(Color.TRANSPARENT)
canvas.drawCircle(debugPoint.x, debugPoint.y, 10f, paint)
canvas.drawLine(debugPoint.x, 0f, debugPoint.x, height.toFloat(), paint)
canvas.drawLine(0f, debugPoint.y, width.toFloat(), debugPoint.y, paint)
canvas.drawText("${debugPoint.x}, ${debugPoint.y}", debugPoint.x, debugPoint.y, paint)
canvas.drawLine(width / 2f, 0f, width / 2f, height.toFloat(), paint2)
canvas.drawLine(0f, height / 2f, width.toFloat(), height / 2f, paint2)
canvas.drawText("${width / 2f}, ${height / 2f}", width / 2f, height / 2f, paint2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment