Several properties of FontMetrics in Android explain the full _android

Source: Internet
Author: User
Tags abs

To talk to you today about Android's understanding of several properties of FontMetrics, the final size of the text in Android is related to the type and font size of the font used to draw the text.

Set Font type Paint.settypeface (typeface typeface)

Set Font size paint.settextsize (float textsize)

Paint.fontmetrics has 5 attributes, and these 5 properties are related to fonts, and the following is an explanation of the official API documentation:


Translate what he means:

1. The Datum point is baseline

2. Ascent is the distance above the baseline to the top of the character

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

4. The leading document is ambiguous, which is actually the distance between the descent of the previous line of characters and the ascent of the next line

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

6. Ibid., bottom refers to the value of the bottom character to the baseline, that is, the maximum value of descent

There is another way to translate online:

Top: Maximum distance of characters above baseline in font

Bottom: The maximum distance between characters in the font beyond the baseline

Ascent: A recommended distance above the baseline for a single character

Descent: A recommended distance above the baseline for a single character

Leading: Standard line spacing

In fact, after the translation is still difficult to understand what the specific meaning, so I find the following map to understand:


Note that the property value obtained above is relative to the baseline coordinate value, not the distance value.

The height of the font can be computed by descent+math.abs (ascent). The width of the string can be obtained by Paint.measuretext ("xxxx"). Note that if the selected font is a equal-width font, the width of each character is the same, and if the font is not equal width, the width of the character is not the same.

The actual visual height and width of the string, that is, the minimum bounding box can be obtained by paint.gettextbounds () or textview.getlinebounds (), as shown in the following illustration:


Paint.gettextbounds

This method gets the rectangular area occupied by the character [string], meaning the rectangular area of the visible part of the font

Rect bound = new Rect ();

Mpaint.gettextbounds (text, 0, text.length (), bound);

Bound.right-bound.left//Gets the width of the rectangular area of the visible part of the character [string]

Paint.measuretext (text)

Returns the width of the character [string], noting that the

Bound.right-bound.left are distinguished from each other.

Because there is usually a part of the blank space on both sides of each character, it is easy to read. So the Measuretext size is usually larger than bound.right-bound.left. So, for a single character, say:

Measuretext = Bound.right-bound.left + character on both sides of the left white width

Let's talk about the calculation of line height:

The height of the line in which the character is located = ascent + descent + leading, which is the height + line spacing of the characters, can be computed by descent+math.abs (ascent) + leading. For the row height in TextView, it can be obtained directly through the Getlineheight () method.

In fact, there is no understanding of the relationship, in the ordinary development of the main use to get the character or string width and height, we just need to know their way to obtain

Width Get method: Paint.measuretext (text)

Height Gain method: Descent+math.abs (Ascent)

Above all is the individual understanding, welcome everybody to spit the trough correction!

The above Android FontMetrics several attributes of a comprehensive explanation is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

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.