Draw a circle with the center (CX, CY) and write a number centered on it:
In general, in drawtext (text, CX, XY, paint), the vertex (CX, CY) is the starting point of the text, as follows:
Even if you use paint. settextalign (paint. Align. Center), the text is horizontally centered, but the vertical text is not centered. If you can get the text height, and then shift down to half, you can center:
First, we need to obtain the text boundary,Paint. gettextbounds (, textbounds );
After the boundary is obtained, the height and width can be calculated.
Paint countpaint = new paint (paint. anti_alias_flag | paint. dev_kern_text_flag); countpaint. setcolor (color. blue); countpaint. settextsize (20f); countpaint. settypeface (typeface. default_bold); countpaint. settextalign (paint. align. center); rect textbounds = new rect (); string numberstr = string. valueof (number); countpaint. gettextbounds (numberstr, 0, numberstr. length (), textbounds); // get text bounds, that can get the text width and heightint textheight = textbounds. bottom-textbounds. top; log. I ("tag", "bounds: Left =" + textbounds. left + ", Right =" + textbounds. right + ", Top =" + textbounds. top + ", Bottom =" + textbounds. bottom); canvas. drawtext (numberstr, CX, Cy + textheight/2, countpaint );