温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android Canvas中drawText()与文字居中的示例分析

发布时间:2021-05-20 11:13:51 来源:亿速云 阅读:145 作者:小新 栏目:移动开发

这篇文章主要介绍Android Canvas中drawText()与文字居中的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

自定义View是绘制文本有三类方法

// 第一类
public void drawText (String text, float x, float y, Paint paint)
public void drawText (String text, int start, int end, float x, float y, Paint paint)
public void drawText (CharSequence text, int start, int end, float x, float y, Paint paint)
public void drawText (char[] text, int index, int count, float x, float y, Paint paint)

// 第二类
public void drawPosText (String text, float[] pos, Paint paint)
public void drawPosText (char[] text, int index, int count, float[] pos, Paint paint)

// 第三类
public void drawTextOnPath (String text, Path path, float hOffset, float vOffset, Paint paint)
public void drawTextOnPath (char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)

其中drawText()最常用,drawPosText ()是根据一个个坐标点指定文字位置,drawTextOnPath ()是根据路径绘制。但drawText()的x,y参数是干嘛的呢?

先来测试下

Paint paint=new Paint();
 paint.setStyle(Paint.Style.FILL);
 paint.setStrokeWidth(12);
 paint.setTextSize(100);

 String text="测试:my text";
 canvas.drawText(text, 200, 400, paint);

 //画两条线标记位置
 paint.setStrokeWidth(4);
 paint.setColor(Color.RED);
 canvas.drawLine(0, 400, 2000, 400, paint);
 paint.setColor(Color.BLUE);
 canvas.drawLine(200, 0, 200, 2000, paint);

Android Canvas中drawText()与文字居中的示例分析

左对齐-left

可以看到,x,y并不是指定文字的中点位置,并且x,y与文字对齐方式有关(通过setTextAlign()指定,默认为left)

Android Canvas中drawText()与文字居中的示例分析

居中对齐-center

Android Canvas中drawText()与文字居中的示例分析

右对齐-right

(为了使文字完整,上面调整了下x,y的值)

从上面三种情况得出结论,x所对应的竖线:

  • 左对齐 — 文字的左边界

  • 居中对齐 — 文字的中心位置

  • 右对齐 — 文字的左边界

y对应的横线并不是文字的下边界,而是基准线Baseline

看下面这张图

Android Canvas中drawText()与文字居中的示例分析

红色的Baseline是基准线,紫色的Top是文字的最顶部,也就是在drawText()中指定的x所对应,橙色的Bottom是文字的底部。

拿这些值如何获取呢?

Paint.FontMetrics fontMetrics=paint.getFontMetrics();
 fontMetrics.top
 fontMetrics.ascent
 fontMetrics.descent
 fontMetrics.bottom

记得要在设置完Paint的文字大小,宽度之类属性后再获取FontMetrics,
baseline对应对应值为0,在它下面的descent和bottom值为正,top和ascent为负。那文字的高度为bottom - top

Android Canvas中drawText()与文字居中的示例分析

所以,实际绘制的时候取决于基线上一个点来绘制文字,而这个点有三种分别对应为left,center,right

Android Canvas中drawText()与文字居中的示例分析

好啦,把drawText()中x,y参数讲清楚后实现文字居中就很容易了

直接上代码

//矩形背景
 Paint bgRect=new Paint();
 bgRect.setStyle(Paint.Style.FILL);
 bgRect.setColor(Color.YELLOW);
 RectF rectF=new RectF(200, 200, 800, 600);
 canvas.drawRect(rectF, bgRect);

 Paint textPaint=new Paint();
 textPaint.setStyle(Paint.Style.FILL);
 textPaint.setStrokeWidth(8);
 textPaint.setTextSize(50);
 textPaint.setTextAlign(Paint.Align.CENTER);

 String text="测试:my text";
 //计算baseline
 Paint.FontMetrics fontMetrics=textPaint.getFontMetrics();
 float distance=(fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
 float baseline=rectF.centerY()+distance;
 canvas.drawText(text, rectF.centerX(), baseline, textPaint);

效果

Android Canvas中drawText()与文字居中的示例分析

将对齐方式设置为center,那要让文字居中显示,x值就为矩形中心x值,y值也就是baseline的计算看下图

Android Canvas中drawText()与文字居中的示例分析

y = 矩形中心y值 + 矩形中心与基线的距离

距离 = 文字高度的一半 - 基线到文字底部的距离(也就是bottom)
 = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

以上是“Android Canvas中drawText()与文字居中的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI