Canvas. drawtext is centered on (CX, CY) and writes

Source: Internet
Author: User
Tags drawtext

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.