画布drawtext方向

Kos*_*uta 9 android canvas rotation

如何使文本垂直写入?如何将文字旋转90度?单独写每个字母是愚蠢的,但现在,我不知道另一种方式.

 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);
    }
Run Code Online (Sandbox Code Playgroud)

Veg*_*ger 28

简单地旋转文本(或其他任何东西)很简单:使用该rotate()方法旋转画布(之后将其旋转回来,否则您绘制的所有内容都会旋转):

canvas.save();
canvas.rotate(90f, 50, 50);
canvas.drawText("Text",50, 50, paint);
canvas.restore();
Run Code Online (Sandbox Code Playgroud)

save()restore()方法分别保存画布的状态,并恢复它.因此,其他绘制元素不会旋转.如果您只想绘制文本,则不需要这两种方法.

如果要将字符串的字符放在彼此之下,则需要分别处理每个字符.首先,您需要获得字体高度,并且在绘制每个字符时,您需要反复增加具有此高度的y坐标.

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;
}
Run Code Online (Sandbox Code Playgroud)