简体   繁体   English

如何在画布Android上将文本中心左对齐

[英]How to align text center to left on canvas Android

I am developing Photography apps, and for that I am using this code: 我正在开发摄影应用程序,为此我使用此代码:

Canvas canvas = new Canvas(bmOverlay);
TextPaint paint = new TextPaint();
paint.setColor(Color.RED);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(50);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
// if the background image is defined in main.xml, omit this line
canvas.drawBitmap(mBitmap, 0, 0, null);
int left = measureTextWidth(paint, InstaTextActivity.CurrentWord);
int top = measureTextHeight(paint, InstaTextActivity.CurrentWord);

left = mBitmap.getWidth() / 2 - InstaTextActivity.textCount / 2;
top = mBitmap.getHeight() / 2 - InstaTextActivity.textCount / 2;
StaticLayout layout = new StaticLayout(InstaTextActivity.CurrentWord, paint, total,
 android.text.Layout.Alignment.ALIGN_NORMAL, (float) 1.0, (float) 0.0, true);
//canvas.save();
canvas.translate(left, top);
layout.draw(canvas);
//canvas.restore();

In this code I am using paint.setTextAlign(Align.CENTER) . 在这段代码中,我使用的是paint.setTextAlign(Align.CENTER) It aligns to centre, but text doesn't align center to left, instead it aligns centre to right. 它与中心对齐,但文本不会从中心向左对齐,而是从中心向右对齐。 Also, what is the proper way to align text on canvas? 另外,在画布上对齐文本的正确方法是什么?

在此输入图像描述

But I want 但我想要

在此输入图像描述

Im using 我正在使用

StaticLayout mTextLayout = new StaticLayout(text, mTextPaint,
                canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
                false);
canvas.translate((canvas.getWidth() / 2) - (mTextLayout.getWidth() / 2), (canvas.getHeight() / 2) - ((mTextLayout.getHeight() / 2)));

to center my multi line text on a canvas 将我的多行文字放在画布上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM