drawText详解

来源:互联网 发布:软件定制开发服务 编辑:程序博客网 时间:2024/05/21 00:20
Canvas 作为绘制文本时,是以基线为基准绘制的,不是左上角

FontMetrics对象
它以四个基本坐标为基准,分别为:
・FontMetrics.top
・FontMetrics.ascent
・FontMetrics.descent

・FontMetrics.bottom

ascent和top都为负数,因为向下为Y轴正方向

示例:

Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG);textPaint.setTextSize( 35);textPaint.setColor( Color.WHITE);// FontMetrics对象FontMetrics fontMetrics = textPaint.getFontMetrics();String text = "abcdefghijklmnopqrstu";// 计算每一个坐标float baseX = 0;float baseY = 100;float topY = baseY + fontMetrics.top;float ascentY = baseY + fontMetrics.ascent;float descentY = baseY + fontMetrics.descent;float bottomY = baseY + fontMetrics.bottom;// 绘制文本canvas.drawText( text, baseX, baseY, textPaint);// BaseLine描画Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);>baseLinePaint.setColor( Color.RED);canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint);// Base描画canvas.drawCircle( baseX, baseY, 5, baseLinePaint);// TopLine描画Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);topLinePaint.setColor( Color.LTGRAY);canvas.drawLine(0, topY, getWidth(), topY, topLinePaint);// AscentLine描画Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);ascentLinePaint.setColor( Color.GREEN);canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint);// DescentLine描画Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);descentLinePaint.setColor( Color.YELLOW);canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint);// ButtomLine描画Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);bottomLinePaint.setColor( Color.MAGENTA);canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);


原创粉丝点击