How to calculate string font width in pixels?

前端 未结 3 480
星月不相逢
星月不相逢 2021-01-04 09:41

I want to calculate string font width in pixels in android. I am having string array and I am appending all these array elements to form one string. so I ha

3条回答
  •  渐次进展
    2021-01-04 10:11

    Looks like there is a measureText method available on Paint. I also found an example:

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setStrokeWidth(5);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setTextSize(64);
    mPaint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
    // ...
    float w = mPaint.measureText(text, 0, text.length());
    

提交回复
热议问题