Android:变成位图的文本不在重心
我想将文本转换为位图,编码如下: 代码: 截图: 问题:Android:变成位图的文本不在重心,android,canvas,text,bitmap,Android,Canvas,Text,Bitmap,我想将文本转换为位图,编码如下: 代码: 截图: 问题: 如何在可用空间的中心绘制文本?谢谢 您可以做的一件事是尝试用XML创建它。尝试将gravity设置为CENTER|u VERTICAL | CENTER|u HORIZONTAL,但另一件事是,在创建XML时,可以将layout_gravity设置为CENTER|u VERTICAL | CENTER|u HORIZONTAL(您也可以通过代码完成,但它更复杂)。而不是 LinearLayout.LayoutParams layout
如何在可用空间的中心绘制文本?谢谢 您可以做的一件事是尝试用XML创建它。尝试将
gravity
设置为CENTER|u VERTICAL | CENTER|u HORIZONTAL
,但另一件事是,在创建XML时,可以将layout_gravity
设置为CENTER|u VERTICAL | CENTER|u HORIZONTAL
(您也可以通过代码完成,但它更复杂)。而不是
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(maxWidth, maxHeight);
tv.setLayoutParams(layoutParams);
tv.setGravity(Gravity.CENTER);
使用
我使用了另一种方法来实现类似的效果:
public Bitmap drawText(int textSize, String textColor, String strength, int textWidth, int maxHeight, String text)
{
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG| Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setAntiAlias(true);
textPaint.setColor(Color.BLACK);
//strength
if (strength.equals("bold"))
{
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
}
textPaint.setTextSize(textSize);
StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// Create bitmap and canvas to draw to
Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
// Draw background
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
c.drawPaint(paint);
// Draw text
c.save();
c.translate(0, 0);
mTextLayout.draw(c);
c.restore();
return b;
}
谢谢你的回复。我也试过
Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL
,但效果是一样的。位图中的文本可能正确地位于中心,但当位图绘制到画布上时,它位于(0,0)处。您知道在画布上绘制位图时如何居中吗?可以尝试更改为LinearLayout.LayoutParams LayoutParams=新建LinearLayout.LayoutParams(LayoutParams.Wrap_内容,LayoutParams.Wrap_内容);tv.setLayoutParams(layoutParams);谢谢。。。您能告诉我如何将其合并到上述代码中吗?Thx…但它仍然位于上面截图中的位置…是否因为位图在画布中没有正确定位?(我在下面使用了另一种方法来进行类似的输出)
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(layoutParams);
tv.setGravity(Gravity.CENTER_VERTICAL |Gravity.CENTER_HORIZONTAL);
public Bitmap drawText(int textSize, String textColor, String strength, int textWidth, int maxHeight, String text)
{
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG| Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setAntiAlias(true);
textPaint.setColor(Color.BLACK);
//strength
if (strength.equals("bold"))
{
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
}
textPaint.setTextSize(textSize);
StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// Create bitmap and canvas to draw to
Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
// Draw background
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
c.drawPaint(paint);
// Draw text
c.save();
c.translate(0, 0);
mTextLayout.draw(c);
c.restore();
return b;
}