The documentation for the following methods of Layout (including StaticLayout, DynamicLayout, and BoringLayout) are very sparce.
getLineBaseline(int line)getLineDescent(int line)getLineAscent(int line)getLineBottom(int line)getLineTop(int line)
Exactly what are the numbers that these methods are returning? Are they the normal font metrics values or are they the locations on the layout?
I made a test project to find out so I am posting my answer below Q&A style.
I have previously described the meaning of top, ascent, baseline, descent, bottom, and leading in Android's FontMetrics.
Because the
LayoutmethodsgetLineBaseline,getLineDescent,getLineAscent,getLineBottom, andgetLineTopsound so similar to theFontMetricsnames, it is easy to get them confused. However, they report two different types of things:These methods return their vertical positions on the layout, which is different for every line.
getLineBaselinegetLineBottomgetLineTopHowever, the following two methods return the value for the particular line they are on, regardless of where the line is in the layout. So unless there are special spans that affect the size, they will be the same for every line.
getLineAscentgetLineDescentDemo
I made a simple project to demonstrate that the imformation above. There are six lines of text in an
EditText. Clicking the button logs the info for each line.Results
Here is the logged result:
As you can see, top, bottom, and baseline are cumulative based on the line. Ascent and descent mainly stay the same for each line. Ascent is equal to
FontMetrics.ascentfor all lines except the first line, where it equalsFontMetrics.top. And descent is equal toFontMetrics.descentfor all lines except the last line, where it equalsFontMetrics.bottom.So top, bottom, baseline, ascent, and descent for a line should not be considered to be equal to the
FontMetricsvalues of the same names. On a line ascent is the distance from the baseline to the bottom of the line above it. Descent is the distance from the baseline to the top of the next line.In the source code, only
topanddescentare saved for every line. The other values are calculated from them:Project code:
See also
LayoutdocumentationLayout.javasource codeStaticLayout.javasource code