Android将文本绘制到中心的矩形并根据需要裁剪 [英] Android draw text into rectangle on center and crop it if needed

查看:115
本文介绍了Android将文本绘制到中心的矩形并根据需要裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在中心(水平和垂直)上将文本绘制成矩形。如果有太多文本无法裁剪,那么该文本将不适合rect。

I want to draw text into rectangle on center (horizontally and vertically). If there is too much of a text that crop it what do not fit into rect.

我已经尝试将其作为此示例显示,但没有运气。

I have try to do it as this example show, but without luck.

有什么想法吗?

推荐答案

尝试一下

private void drawRectText(String text, Canvas canvas, Rect r) {

    textPaint.setTextSize(20);
    textPaint.setTextAlign(Align.CENTER);
    int width = r.width();

    int numOfChars = textPaint.breakText(text,true,width,null);
    int start = (text.length()-numOfChars)/2;
    canvas.drawText(text,start,start+numOfChars,r.exactCenterX(),r.exactCenterY(),textPaint);
}

这篇关于Android将文本绘制到中心的矩形并根据需要裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆