简体   繁体   English

Android 油漆。 字符宽度是如何计算的?

[英]Android Paint. How is the character width calculated?

I need to know how the width of text changes when I increase paint.getTextSize() n times.我需要知道当我增加paint.getTextSize() n 次时文本的宽度如何变化。 I thought that is proportional, but first test indicates that it is not.我认为这是成比例的,但第一次测试表明它不是。 I got result like below, where我得到如下结果,其中

  • 1st number - size of text set by Paint.setTextSize(float)第一个数字- 由Paint.setTextSize(float)设置的文本大小

  • 2nd number - width of text measured with Paint.measureText(String)第二个数字- 用Paint.measureText(String)测量的文本宽度

     1;1.0 2;1.0 3;2.0 4;2.0 5;3.0 6;3.0 7;4.0 8;4.0 9;5.0 10;5.0 11;6.0 12;7.0 13;7.0 14;8.0 15;8.0 16;9.0 17;9.0 18;10.0 19;10.0 20;11.0 21;11.0 22;12.0 23;13.0 24;13.0 25;14.0 26;14.0 27;15.0 28;15.0 29;16.0 30;16.0 31;17.0 32;17.0 33;18.0 34;19.0 35;19.0 36;20.0 37;20.0 38;21.0 39;21.0 40;22.0 41;22.0 42;23.0 43;23.0 44;24.0 45;24.0 46;25.0 47;26.0 48;26.0 49;27.0 50;27.0 51;28.0 52;28.0 53;29.0

Is it possible to calculate how the text width would change after changing text size n times?是否可以计算更改文本大小n次后文本宽度将如何变化?
I don't want to use Paint.measureText(String) method ever time i change size, because it's bad for performance.我不想每次更改大小时都使用Paint.measureText(String)方法,因为这对性能不利。
Thanks in advance!提前致谢!

See Getting height of text view before rendering to layout .请参阅在渲染到布局之前获取文本视图的高度 Maybe it will measure text faster.也许它会更快地测量文本。 An example for height is below.高度的示例如下。

public static int getHeight(TextView t) {
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(screenWidth(t.getContext()), View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    t.measure(widthMeasureSpec, heightMeasureSpec);
    return t.getMeasuredHeight();
}

public static int screenWidth(Context context)
{
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    return display.getWidth();
}

Also you can try to estimate a formula of changing.您也可以尝试估计变化的公式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM