1

以下方法Layout(包括StaticLayoutDynamicLayoutBoringLayout)的文档非常少。

这些方法返回的具体数字是多少?它们是正常的字体度量值还是布局上的位置?

我做了一个测试项目来找出答案,所以我在问答风格下发布了我的答案。

4

1 回答 1

6

我之前在 Android 的 FontMetrics 中描述了 top、ascent、baseline、descent、bottom 和leading 的含义

因为Layout方法getLineBaseline, getLineDescent, getLineAscent, getLineBottom, 和getLineTop听起来很像FontMetrics名字,所以很容易混淆。但是,他们报告了两种不同类型的事情:

这些方法返回它们在 layout 上的垂直位置,每行都不同。

  • getLineBaseline
  • getLineBottom
  • getLineTop

但是,以下两种方法返回它们所在的特定的值,而不管该行在布局中的什么位置。因此,除非有影响大小的特殊跨度,否则它们对于每一行都是相同的。

  • getLineAscent
  • getLineDescent

演示

我做了一个简单的项目来证明上面的信息。一个文件中有六行文本EditText。单击该按钮会记录每行的信息。

在此处输入图像描述

结果

这是记录的结果:

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

如您所见,顶部、底部和基线是基于线累积的。每条线路的上升和下降主要保持不变。AscentFontMetrics.ascent对除第一行以外的所有行都等于 ,它等于FontMetrics.topFontMetrics.descent除最后一行外,所有行的下降都等于FontMetrics.bottom

因此,一条线的顶部、底部、基线、上升和下降不应被视为等于FontMetrics相同名称的值。线上的上升是从基线到其上方线的底部的距离。下降是从基线到下一行顶部的距离。

在源代码中,只有topdescent被保存为每一行。其他值是根据它们计算的:

  • 底部 = 下一行的顶部
  • 基线 = 底部 - 下降
  • 上升 = 顶部 - (底部 - 下降)

项目代码:

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);
    }
}

也可以看看

于 2017-04-29T03:07:40.233 回答