Guowei Lv

1 minute read

Today we are going to write a custom view that count your steps.

Screen Shot

There are 2 parts to draw: the arcs and the text.

The arcs are easy to draw, only thing to keep in mind is to remember the border width.

The tricky part is to draw the text.

Suppose we want the text to be in the center of the view.

Since the canvas.drawText() will use the baseline to draw. That means we need to figure out the dy from the center to the baseline.

This is how to do it:

val fontMetrics = textPaint.fontMetricsInt
val dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom

The full source code

comments powered by Disqus