我目前正在创建一个图像编辑器,并尝试使用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)