安卓drawText中的坑

安卓中在canvas.drawText()时发现很多需要注意的问题:

Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setTextAlign(Paint.Align.LEFT);
//mPaint.setTextAlign(Paint.Align.CENTER);

初始化Paint对象时可以指定文本对齐方式,先讨论默认情况(也就是Paint.Align.LEFT)。
通过Paint对象可以获得与文本相关的两个重要的对象:

//获取可包裹文本的最小矩形(left,top,right,bottom指的是相对基准点的值,而不是实际的矩形的位置坐标)
Rect bounds=new Rect();
mPaint.getTextBounds(text,0,text.length(),bounds);
//获取FontMetrics对象
FontMetrics mFontMetrics = mPaint.getFontMetrics();

FontMetrics对象在安卓中的解释:
这里写图片描述

为了能直观的看出这些值的含义,我将这些值的相对位置画了出来:
这里写图片描述
相应的参数解释如下图:
这里写图片描述
无论是bounds(left,top,right,bottom)还是fontMetrics(top,ascent,descent,bottom)都是相对于基准点的数值差

因此,在绘制文本,并且要求文本中心在(x,y)处时,可以如下处理:

public class DrawTextUtil {
    public static void drawText(Canvas canvas, String text, int x,int y,Paint paint){
//将基准点放在文本的中心(仍在基准线上)
        paint.setTextAlign(Paint.Align.CENTER);
        Rect bounds=new Rect();
        paint.getTextBounds(text,0,text.length(),bounds);
//求出基准线y坐标的值,y-bounds.height()/2得到文本最上沿的y坐标,再-bounds.top地到基准线y坐标
        int realY=y-bounds.height()/2-bounds.top;
        canvas.drawText(text,x,realY,paint);
    }
}

最后,附上绘制对应线的代码

public class DrawText extends View {
    String text="dghiwk";
    private Paint mPaint;
    private Rect bounds;
    private int drawX=100;
    private int drawY=300;
    private Paint.FontMetrics mFontMetrics;

    public DrawText(Context context) {
        super(context);
        init(context);
    }

    public DrawText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DrawText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public DrawText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr);
    }
    public void init(Context context){
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(200);
        mPaint.setTextAlign(Paint.Align.LEFT);
        bounds=new Rect();
        mPaint.getTextBounds(text,0,text.length(),bounds);
        mFontMetrics = mPaint.getFontMetrics();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制文本
        canvas.drawText(text,drawX,drawY,mPaint);
        //top相对位置
        mPaint.setColor(Color.RED);
        canvas.drawLine(drawX,drawY+mFontMetrics.top,drawX+bounds.right,drawY+mFontMetrics.top,mPaint);
        //ascent相对位置
        mPaint.setColor(Color.BLUE);
        canvas.drawLine(drawX,drawY+mFontMetrics.ascent,drawX+bounds.right,drawY+mFontMetrics.ascent,mPaint);
        //descent相对位置
        mPaint.setColor(Color.GRAY);
        canvas.drawLine(drawX,drawY+mFontMetrics.descent,drawX+bounds.right,drawY+mFontMetrics.descent,mPaint);
        //bottom相对位置
        mPaint.setColor(Color.MAGENTA);
        canvas.drawLine(drawX,drawY+mFontMetrics.bottom,drawX+bounds.right,drawY+mFontMetrics.bottom,mPaint);
        //bounds相对位置
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(drawX+bounds.left,drawY+bounds.top,drawX+bounds.right,drawY+bounds.bottom,mPaint);
        //基准点位置
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(drawX,drawY,2,mPaint);
        //基准线
        mPaint.setColor(Color.GRAY);
        canvas.drawLine(drawX,drawY,drawX+bounds.right,drawY,mPaint);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值