问题描述
如何使文字垂直书写?如何将文字旋转90度?
单独写每个字母是愚蠢的,但是现在,我不知道另一种方式。
how to make that text was written vertically? how to rotate text 90 degrees?Write each letter individually is stupid, but now ,i don't know another way.
Paint paint = new Paint();
public DrawView(Context context, double arr[])
{
super(context);
paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas)
{
canvas.drawText("Test",50, 50, paint);
}
推荐答案
只需旋转文本(或其他任何内容)其他)很容易:使用方法来旋转画布(之后将其旋转回来,否则您绘制的所有内容都会旋转):
Simply rotating text (or anything else) is easy: Use the rotate()
method to rotate the canvas (afterwards it is rotated back, otherwise everything you draw becomes rotated):
canvas.save();
canvas.rotate(90f, 50, 50);
canvas.drawText("Text",50, 50, paint);
canvas.restore();
save()
和 restore()
方法分别保存并恢复画布的状态。因此,其余绘制的元素不会旋转。如果只想绘制文本,则不需要使用这两种方法。
The save()
and restore()
methods respectively save the state of the canvas and restores it. So the rest of your drawn elements are not rotated. If you only want to paint the text these two methods are not necessary.
如果要将字符串的字符置于彼此之下,则需要处理每个字符分别。首先,您需要获取字体高度,绘制每个字符时,需要一遍又一遍地增加此高度的y坐标。
If you want to put the characters of the string under each other, you need to process each character separately. First you'd need to obtain the font height and when drawing each character you need to increase the y-coordinate with this height over and over again.
int y = 50;
int fontHeight = 12; // I am (currently) too lazy to properly request the fontHeight and it does not matter for this example :P
for(char c: "Text".toCharArray()) {
canvas.drawText(c, 50, y, paint);
y += fontHeight;
}
这篇关于画布drawtext方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!