Android 如何在不显示文字时获取文字宽度

Android 如何在不显示文字时获取文字宽度

正确方式:

在canvas中使用:paint.measureText(text)
在TextView中使用:textView.getPaint()..measureText(text)

代码示例:

package com.demo.myapplication3;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextPaint;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        canvasMeasure();
        textViewMeasure();
    }


    private void canvasMeasure() {
        String text = "测试:XYZ xyz";

        Bitmap bitmap = Bitmap.createBitmap(800, 1000, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(bitmap, 0, 0, null);

        Paint paint = new Paint();
        paint.setTextSize(30);
        canvas.drawText(text, 100, 100, paint);

        //1. 粗略计算文字宽度
        Log.d(TAG, "measureText=" + paint.measureText(text));

        //2. 计算文字所在矩形,可以得到宽高
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        int w = rect.width();
        int h = rect.height();
        Log.d(TAG, "w=" + w + "  h=" + h);

        //3. 精确计算文字宽度
        int textWidth = getTextWidth(paint, text);
        Log.d(TAG, "textWidth=" + textWidth);
    }

    private static int getTextWidth(Paint paint, String str) {
        int iRet = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }


    private void textViewMeasure() {
        String text = "测试:XYZ xyz";
        TextView textView = findViewById(R.id.tv_test);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
        textView.setText(text);

        int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        textView.measure(spec, spec);

        // getMeasuredWidth
        int measuredWidth = textView.getMeasuredWidth();

        // new textpaint measureText
        TextPaint newPaint = new TextPaint();
        newPaint.setTextSize(30);
        float newPaintWidth = newPaint.measureText(text);

        // textView getPaint measureText
        TextPaint textPaint = textView.getPaint();
        float textPaintWidth = textPaint.measureText(text);

        Log.e(TAG, "测试字符串:" + text);
        Log.e(TAG, "getMeasuredWidth:" + measuredWidth);
        Log.e(TAG, "newPaint measureText:" + newPaintWidth);
        Log.e(TAG, "textView getPaint measureText:" + textPaintWidth);

    }

    //    print
    //2021-04-11 12:51:50.428 15976-15976/com.demo.myapplication3 D/MainActivity: measureText=202.0
    //2021-04-11 12:51:50.429 15976-15976/com.demo.myapplication3 D/MainActivity: w=200  h=30
    //2021-04-11 12:51:50.429 15976-15976/com.demo.myapplication3 D/MainActivity: textWidth=202
    //2021-04-11 12:51:50.430 15976-15976/com.demo.myapplication3 E/MainActivity: 测试字符串:测试:XYZ xyz
    //2021-04-11 12:51:50.430 15976-15976/com.demo.myapplication3 E/MainActivity: getMeasuredWidth:202
    //2021-04-11 12:51:50.430 15976-15976/com.demo.myapplication3 E/MainActivity: newPaint measureText:202.0
    //2021-04-11 12:51:50.430 15976-15976/com.demo.myapplication3 E/MainActivity: textView getPaint measureText:202.0
}

上一篇:带扩展标记的TextView


下一篇:TestView