一个有希望的快速问题,但我似乎无法找到任何示例......我想View通过a 将多行文本写入自定义Canvas,并且onDraw()我有:
...
String text = "This is\nmulti-line\ntext";
canvas.drawText(text, 100, 100, mTextPaint);
...
Run Code Online (Sandbox Code Playgroud)
我希望这会导致换行,但相反,我会看到神秘的角色\n.
任何指针赞赏.
保罗
我目前正在创建一个图像编辑器,并尝试使用canvas.drawText()在图像上绘制文本.到目前为止,我已经成功地做到了这一点,但是当用户输入的文本太长时,文本只会继续在页面外的一行上,并且不会将自身包裹到屏幕的宽度.我该怎么做呢?我尝试过使用静态布局,但似乎无法让它工作,有没有人有一个教程来做到这一点?
我使用静态布局绘制画布的功能:
public Bitmap createImage(float scr_x,float scr_y,String user_text){
Canvas canvas = new Canvas(image);
scr_x = 100;
scr_y = 100;
final TextPaint tp = new TextPaint(Color.WHITE);
canvas.save();
StaticLayout sl = new StaticLayout("" + user_text, tp, originalBitmap.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
sl.draw(canvas);
return image;
}
Run Code Online (Sandbox Code Playgroud)
好的,我已经更新了我的代码,但是当我尝试在图像上绘制时根本没有任何事情发生,我不知道为什么:
public Bitmap createImage(String user_text) {
// canvas object with bitmap image as constructor
Canvas canvas = new Canvas(image);
TextPaint tp = new TextPaint();
tp.setColor(Color.RED);
tp.setTextSize(50);
tp.setTextAlign(Align.CENTER);
tp.setAntiAlias(true);
StaticLayout sl = new StaticLayout("" + user_text, tp,
canvas.getWidth(), …Run Code Online (Sandbox Code Playgroud) 我正在做一个 Android 游戏,我正在使用这样的函数在设备屏幕上显示文本:
public void drawString(String text, int x, int y, Paint paint) {
canvas.drawText(text, x, y, paint);
}
Run Code Online (Sandbox Code Playgroud)
我尝试显示以下消息:
g.drawString("Player: " + playerString+ " :\n" + messageString,SCREENWIDTH / 2, SCREENHEIGHT / 2, paint);
Run Code Online (Sandbox Code Playgroud)
然而,我得到的是一个奇怪的字符(一个正方形),而不是换行符(\n)。
有人可以帮助我吗?
谢谢