Android DrawText to achieve absolute Center text

Source: Internet
Author: User
Tags drawtext


We often encounter custom components in Android, because the existing Android components are often not enough to meet the needs of the present, today we will introduce you to the custom build process used in the DrawText of the problem is not centered solution

First, let's look at this diagram:

This is a full text of the area map, the complete Android in the DrawText of the time of the writing rules, and what are these areas represented?

1. Datum Point is Baseline

2. Ascent is the distance above the baseline to the highest character

3. Descent is the distance below baseline to the lowest character

4. The leading document is very vague, in fact, the distance between the descent of the last line of characters and the ascent of the next line

5. Top refers to the value of the highest character to baseline, which is the maximum value of ascent

6. Ibid., bottom refers to the value of the most down character to baseline, which is the maximum value of descent

Where baseline is our call DrawText when a parameter, but also in the drawtext of the time of a baseline and reference line, the following is the time of the DrawText need to pass the parameters, according to the comments we can not be difficult to understand the meaning of the fields represented by each:

Text: The words to be written up

x: The coordinates of the starting point x of the Write

Y:baseline's coordinates

Paint: Brushes

Here's a look at this picture:

The blue horizontal line refers to the midpoint of the target rectangle that draws the text: targetrect.centery () = (Targetrect.bottom + targetrect.top)/2

Textcenterverticalbaseliney is the y-coordinate at which the text is drawn, and targetrect.centery () is half the height of the control. In fact, this conversion relationship is not difficult to understand, targetrect.centery ()-fm.descent means to raise the entire text area to 1/2 of the control, and then we add (fm.descent-fm.ascent)/ 2 means that text sinking text is descent to half the length of ascent, thus achieving vertical center of text. , it is not difficult to see that the calculation formula should be:

Textcenterverticalbaseliney = Targetrect.centery () + (fm.descent-fm.ascent)/2-fm.descent;

Hope to be able to help everyone

Reference Original: http://blog.csdn.net/wan778899/article/details/51460849

Android DrawText to achieve absolute Center text

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.