Mus*_*ser 14 android canvas word-wrap drawtext
我目前正在创建一个图像编辑器,并尝试使用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(), Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
canvas.translate(100, 100);
sl.draw(canvas);
return image;
}
Run Code Online (Sandbox Code Playgroud)
staticlayout不是用来在画布上画画吗?
Lis*_*ray 13
是的,StaticLayout 就是您用来在Canvas上绘制多行文本的用途.拯救自己一个痛苦的世界,不要考虑自己打破文本 - 你正走在正确的道路上.我不确定位图问题,但上面的第二个代码可以很好地在画布上为我绘制文本.
这是在画布上绘制布局的方法:http: //developer.android.com/reference/android/text/Layout.html#draw(android.graphics.Canvas)
小智 6
public Bitmap drawMultilineTextToBitmap(Context gContext,
int gResId,
String gText) {
// prepare canvas
Resources resources = gContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if(bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
// new antialiased Paint
TextPaint paint=new TextPaint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.rgb(61, 61, 61));
// text size in pixels
paint.setTextSize((int) (14 * scale));
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
// set text width to canvas width minus 16dp padding
int textWidth = canvas.getWidth() - (int) (16 * scale);
// init StaticLayout for text
StaticLayout textLayout = new StaticLayout(
gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// get height of multiline text
int textHeight = textLayout.getHeight();
// get position of text's top left corner
float x = (bitmap.getWidth() - textWidth)/2;
float y = (bitmap.getHeight() - textHeight)/2;
// draw text to the Canvas center
canvas.save();
canvas.translate(x, y);
textLayout.draw(canvas);
canvas.restore();
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
来源:http : //www.skoumal.net/en/android-drawing-multiline-text-on-bitmap/