Skip to content

Instantly share code, notes, and snippets.

@Krishan14sharma
Last active November 11, 2017 13:48
Show Gist options
  • Save Krishan14sharma/5e116565b8659228381ba8f85f967849 to your computer and use it in GitHub Desktop.
Save Krishan14sharma/5e116565b8659228381ba8f85f967849 to your computer and use it in GitHub Desktop.
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
drawVerticalLines(canvas)
drawHorizontalLines(canvas)
drawSquareStates(canvas) // <------- here
if (touching) {
drawHighlightRectangle(canvas)
}
}
private fun drawSquareStates(canvas: Canvas) {
for ((i, textArray) in squareData.withIndex()) {
for ((j, text) in textArray.withIndex()) {
if (text.isNotEmpty()) {
drawTextInsideRectangle(canvas, squares[i][j], text)
}
}
}
}
// created in the previous post
private fun drawTextInsideRectangle(canvas: Canvas, rect: Rect, str: String) {
val xOffset = textPaint.measureText(str) * 0.5f
val yOffset = textPaint.fontMetrics.ascent * -0.4f
val textX = (rect.exactCenterX()) - xOffset
val textY = (rect.exactCenterY()) + yOffset
canvas.drawText(str, textX, textY, textPaint)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment