FontMetrics
can measure height
Analysis of several important attributes of FontMetrics
1.ascent
2.descent
3.top
4.bottom
5.leading
6.baseline understand this is the focus can be understood as the X axis
Note the characteristics of the Android coordinate system y-axis downward is positive direction upward is negative direction
The second and third parameters of this method determine the location of the baseline.
The best way to understand these parameters is through the English four-wire grid.
The actual measurement reveals that the relationship between these properties is actually fixed and does not have anything to do with the specific writing content ( for English )
The baseline position corresponds to the third line in the four line
Ascent's position is equal to the four line with F, he's fixed at the top of F.
The position of the descent is equivalent to the G in the four line, which is fixed at the bottom of G.
The top position corresponds to the baseline to the top line of the four line.
The position of the bottom is equivalent to the baseline to the four-line bottom line.
(see a lot of people say Top is a Ascent the maximum value, Bottom is a Descent the maximum value. Pass the test I think this is not very good, but at last I see oneself understand.
So the height of the measurement text only needs descent-ascent (coordinate system algorithm)
Paint provides two ways
1.ascent ()
2.descent ()
So you can get the text height by mpaint.ascent () + mpaint.descent ()
The above describes how to get the height of text through FontMetrics, and the following describes the width of the text.
It's easy to get the width of the text. There is just one place to look.
Paintprovides a way
Measuretext ()
This way, you can easily get to the width of the text
It is important to note the acquisition of Paint (TextView)
Through Getpaint (); Get Paint to go through Measuretext again (); This method to get the width of the text can be
If you can call Measuretext () directly from the Paint in the custom view. This method to get the width of the text
Here's a way to get the width and height of the text at the same time
Paintprovides a way
Gettextbounds ()
This method needs to provide a parameter rect rectangle area, which passes the text area to the rect
RectTwo methods are available
Width (); You can get the width of the text
Height (); Can get the height of the text
However, the result of these two methods is that the value of the int type is inaccurate on the accuracy.
Click to open link
Measurement of Android Text size (wide height)