获取文本宽度
方法一:先绘制文本所在的矩形区域,再获取矩形区域的宽度
Rect rect = new Rect();
mPaint.getTextBounds(text,0,text.length(), rect);
canvas.drawRect(rect, mPaint);
float textWidth1 = rect.width();
Log.d("111","textWidth:"+textWidth1);
上述方法由于矩形边框紧贴文字,所有没有多余的空间。
方法二:通过Paint的measureText方法直接测量文本宽度
float textWidth2 = mPaint.measureText(text);
Log.d("111", "textWidth:"+textWidth2);
canvas.drawLine(0, rect.bottom + 20, textWidth2, rect.bottom + 20, mPaint);
此方法计算出的宽度会加上开始和结尾的空间,这个空间就是文字和文字之间的空间,为了美观而存在。如果加上下划线会比较直观。
方法三:计算出每个文字的宽度,最后计算总和
float[] textWidths =new float[text.length()];
mPaint.getTextWidths(text, textWidths);
float textWidth1 =0;
for(int i=0;i<textWidths.length;i++){
textWidth1 = textWidth1 + textWidths[i];
}
Log.d("111","textWidth:"+textWidth1);
此方式计算出的宽度与方法二一样。
TextPaint textPaint =new TextPaint();
textPaint.setTextSize(280);
textPaint.setColor(Color.BLUE);
textPaint.setStrokeWidth(8);
textPaint.setAntiAlias(true);
textPaint.setStyle(Paint.Style.FILL);
float textWidth1 = Layout.getDesiredWidth(text,textPaint);
Log.d("111","textWidth:"+textWidth1);
方法计算出的宽度与前两者一致。
获取文本高度
在控件上绘制文字时,经常要考虑到文本的中心位置,这就需要计算并获取整个文本的宽度和高度。
看图了解一下字体基准线
上面所有的属性都被封装在FontMetrics类中,通过它可以获取并计算文本的宽高
top:在一个大小确定的字体中,被当做最高字形,基线(base)上方的最大距离。
ascent:单行文本中,在基线(base)上方被推荐的距离。
descent:单行文本中,在基线(base)下方被推荐的距离。
bottom:在一个大小确定的字体中,被当做最低字形,基线(base)下方的最大距离。
如果我们想要计算这个文字的高度,只需要使用(descent-ascent)
通过上面的图片对基准线有了大致的了解,具体的代码是
private void init(){
mPaint = new Paint();
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(8);
mPaint.setTextSize(280);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.translate(200,400);
mPaint.setColor(Color.parseColor("#65A21F"));
canvas.drawLine(-200,0, canvas.getWidth()-200,0, mPaint);
canvas.drawLine(0,-400,0, canvas.getHeight()-400, mPaint);
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(2);
mPaint.setStyle(Paint.Style.STROKE);
Rect rect = new Rect();
mPaint.getTextBounds(text,0,text.length(), rect);
canvas.drawRect(rect, mPaint);
float textHeight1 = rect.height();
Log.d("111","textHeight:"+textHeight1);
float textWidth = mPaint.measureText(text);//获取文本的宽度
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(2);
mPaint.setColor(Color.RED);
canvas.drawLine(0, fontMetrics.ascent, mPaint.measureText(text), fontMetrics.ascent, mPaint);
mPaint.setTextSize(50);
canvas.drawText("ascent", textWidth, fontMetrics.ascent, mPaint);
mPaint.setColor(Color.parseColor("#6EAD24"));
canvas.drawLine(0, fontMetrics.bottom, textWidth, fontMetrics.bottom, mPaint);
mPaint.setTextSize(50);
canvas.drawText("bottom", textWidth, fontMetrics.bottom +30, mPaint);
mPaint.setColor(Color.parseColor("#13AA9C"));
canvas.drawLine(0, fontMetrics.descent, textWidth, fontMetrics.descent, mPaint);
mPaint.setTextSize(50);
canvas.drawText("descent", textWidth, fontMetrics.descent, mPaint);
mPaint.setColor(Color.parseColor("#9C17B3"));
canvas.drawLine(0,0, textWidth,0, mPaint);
mPaint.setTextSize(50);
canvas.drawText("baseline", textWidth, fontMetrics.leading, mPaint);
mPaint.setColor(Color.parseColor("#DF3A72"));
canvas.drawLine(0, fontMetrics.top, textWidth, fontMetrics.top, mPaint);
mPaint.setTextSize(50);
canvas.drawText("top", textWidth, fontMetrics.top, mPaint);
mPaint.setTextSize(280);
mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(8);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawText(text,0,0, mPaint);}
方法一:先绘制文本所在的矩形区域,再获取矩形区域的高度
Rect rect = new Rect();
mPaint.getTextBounds(text,0,text.length(), rect);
canvas.drawRect(rect, mPaint);
float textHeight1 = rect.height();
Log.d("111","textHeight:"+textHeight1);
方法二: 通过getFontMetrics()或getFontMetricsInt()获取测量数据
想要计算高度只需要 descent-ascent文章来源:https://www.toymoban.com/news/detail-488954.html
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
float height1 = fontMetrics.descent - fontMetrics.ascent;
Log.d("111", "文本的推荐高度:"+height1);
float height2 = fontMetrics.bottom - fontMetrics.top;
Log.d("111", "文本的最大高度:"+height2);
方法三:通过getFontSpacing实现文章来源地址https://www.toymoban.com/news/detail-488954.html
float height3 = mPaint.getFontSpacing();
Log.d("aaa", "文本的高度:"+height3);
到了这里,关于Android获取文本的宽度和高度的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!