canvas.drawText("hello world",0,0,new Paint()); // 以上代码不显示任何文本信息 注意 该方法的 两个参数, 第一个是 字符串 左边 的x 坐标(left), 第二个是 。。。 底部 的y 坐标(botton而不是top),
如果想让字符串按照自己的意愿显示正常, 需要
canvas.drawText("hello world", 0, GraphicsUtils.getFontHeight(null), new Paint());
其中 GraphicsUtils.getFontHeight(null) 是自定义的方法 代码附上
/** * 获取字体高度 * @param textSize : textSize==null时 获取字符串的默认字体, */ public static int getFontHeight(Integer textSize){ Paint paint = new Paint(); if(textSize != null){ paint.setTextSize(textSize); } FontMetrics fm = paint.getFontMetrics(); return (int)(fm.descent - fm.ascent); }
自定义字体高度在Canvas绘制文本实现
本文详细解释了如何在Canvas中通过自定义方法获取字体高度,并使用该高度来正确绘制文本,解决了文本位置不对齐的问题。
197

被折叠的 条评论
为什么被折叠?



