canvas.drawText(string, xPos, yPos, paint)文本居中的办法

  paintText = new Paint();
        paintText.setStrokeWidth(1);
        paintText.setAntiAlias(true);
        paintText.setColor(Color.RED);
        mTextSize = 700;
        paintText.setTextSize(mTextSize);
        count = "|";//count = "——",为了测试文字是否居中的专用字符
        paintText.setTextAlign(Paint.Align.CENTER);//这句话表明画的时候,以文本中间为基准坐标(仅限于X坐标,Y坐标不能靠这句话搞定)

        int xPos = (canvas.getWidth() / 2);//表明文本的绘制X坐标以该坐标为中心
        int yPos = (int) ((canvas.getHeight() / 2) - ((paintText.descent() + paintText.ascent()) / 2)) ;//得到文本绘制的Y坐标

        canvas.drawText(count, xPos, yPos, paintText);

paintText.ascent():

Return the distance above (negative) the baseline
(ascent) based on the current typeface and text size.
返回:以当前文字字体和文字尺寸的为基准,在baseline上方的距离


paintText.descent();与上面相反


效果图:

这里写图片描述

这里写图片描述

这里写图片描述
字符“——”

这里写图片描述
字符“|”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值