gpt4 book ai didi

java - 如何在 Android Canvas 上将文本中心向左对齐

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:18 24 4
gpt4 key购买 nike

我正在开发摄影应用,为此我使用了以下代码:

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();

在这段代码中,我使用了 paint.setTextAlign(Align.CENTER)。它居中对齐,但文本不居中向左对齐,而是居中向右对齐。另外,在 Canvas 上对齐文本的正确方法是什么?

enter image description here

但是我想要

enter image description here

最佳答案

我正在使用

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)));

让我的多行文本在 Canvas 上居中

关于java - 如何在 Android Canvas 上将文本中心向左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13304199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com