带多行的画布drawtext [英] canvas drawtext with multiline
本文介绍了带多行的画布drawtext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发图像评论应用程序.我用 canvas.drawText(text,x,y,imgPaint);
I am developing a image commenting application. I draw text in canvas with canvas.drawText(text, x, y, imgPaint);
这将显示在一行中.当文本超过画布宽度时,我需要将行换成多行
This appears in a single line. I need to break the line to multiline when the text crosses the canvas width
预先感谢
推荐答案
您需要使用 StaticLayout
:
TextPaint mTextPaint=new TextPaint();
StaticLayout mTextLayout = new StaticLayout("my text\nNext line is very long text that does not definitely fit in a single line on an android device. This will show you how!", mTextPaint, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.save();
// calculate x and y position where your text will be placed
textX = 100;
textY = 100;
canvas.translate(textX, textY);
mTextLayout.draw(canvas);
canvas.restore();
这篇关于带多行的画布drawtext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文