[英]Android Layout: meaning of getLineBaseline, getLineDescent, getLineAscent, getLineBottom, getLineTop
The documentation for the following methods of Layout
(including StaticLayout
, DynamicLayout
, and BoringLayout
) are very sparce. 以下
Layout
方法(包括StaticLayout
, DynamicLayout
和BoringLayout
)的文档非常BoringLayout
。
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 . 之前,我已经在Android的FontMetrics中描述了top,ascent,baseline,descent,bottom和Lead的含义 。
Because the Layout
methods getLineBaseline
, getLineDescent
, getLineAscent
, getLineBottom
, and getLineTop
sound so similar to the FontMetrics
names, it is easy to get them confused. 由于
Layout
方法的getLineBaseline
, getLineDescent
, getLineAscent
, getLineBottom
和getLineTop
声音与FontMetrics
名称非常相似,因此很容易混淆它们。 However, they report two different types of things: 但是,他们报告了两种不同类型的事物:
These methods return their vertical positions on the layout , which is different for every line. 这些方法返回其在布局上的垂直位置 ,每行的位置不同。
getLineBaseline
getLineBottom
getLineTop
However, 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.
因此,除非有影响大小的特殊跨度,否则每行它们都将相同。
getLineAscent
getLineDescent
I made a simple project to demonstrate that the imformation above. 我做了一个简单的项目来证明上面的信息。 There are six lines of text in an
EditText
. EditText
有六行文本。 Clicking the button logs the info for each line. 单击该按钮将记录每行的信息。
Results 结果
Here is the logged result: 这是记录的结果:
line 0 baseline: 67
line 1 baseline: 140
line 2 baseline: 213
line 3 baseline: 286
line 4 baseline: 359
line 5 baseline: 432
line 0 descent: 15
line 1 descent: 15
line 2 descent: 15
line 3 descent: 15
line 4 descent: 15
line 5 descent: 18
line 0 ascent: -67
line 1 ascent: -58
line 2 ascent: -58
line 3 ascent: -58
line 4 ascent: -58
line 5 ascent: -58
line 0 top: 0
line 1 top: 82
line 2 top: 155
line 3 top: 228
line 4 top: 301
line 5 top: 374
line 0 bottom: 82
line 1 bottom: 155
line 2 bottom: 228
line 3 bottom: 301
line 4 bottom: 374
line 5 bottom: 450
FontMetrics top: -67
FontMetrics bottom: 18
FontMetrics ascent: -58
FontMetrics descent: 15
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.ascent
for all lines except the first line, where it equals FontMetrics.top
. 上升等于
FontMetrics.ascent
为除了第一行,其中它等于所有行FontMetrics.top
。 And descent is equal to FontMetrics.descent
for all lines except the last line, where it equals FontMetrics.bottom
. 和下降等于
FontMetrics.descent
为除最后一行,它等于所有线路FontMetrics.bottom
。
So top, bottom, baseline, ascent, and descent for a line should not be considered to be equal to the FontMetrics
values of the same names. 因此,一行的顶部,底部,基线,上升和下降不应被视为等于相同名称的
FontMetrics
值。 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 top
and descent
are saved for every line. 在源代码中,每行仅保存
top
和descent
。 The other values are calculated from them: 其他值是根据它们计算得出的:
Project code: 项目代码:
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
}
public void buttonClick(View view) {
Layout layout = editText.getLayout();
for (int i = 0; i < layout.getLineCount(); i++) {
int baseline = layout.getLineBaseline(i);
Log.i("TAG", "line " + i + " baseline: " + baseline);
}
for (int i = 0; i < layout.getLineCount(); i++) {
int descent = layout.getLineDescent(i);
Log.i("TAG", "line " + i + " descent: " + descent);
}
for (int i = 0; i < layout.getLineCount(); i++) {
int ascent = layout.getLineAscent(i);
Log.i("TAG", "line " + i + " ascent: " + ascent);
}
for (int i = 0; i < layout.getLineCount(); i++) {
int top = layout.getLineTop(i);
Log.i("TAG", "line " + i + " top: " + top);
}
for (int i = 0; i < layout.getLineCount(); i++) {
int bottom = layout.getLineBottom(i);
Log.i("TAG", "line " + i + " bottom: " + bottom);
}
Paint.FontMetricsInt fm = editText.getLayout().getPaint().getFontMetricsInt();
Log.i("TAG", "fm top: " + fm.top);
Log.i("TAG", "fm bottom: " + fm.bottom);
Log.i("TAG", "fm ascent: " + fm.ascent);
Log.i("TAG", "fm descent: " + fm.descent);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.