Home » Android » android – canvas drawtext direction

android – canvas drawtext direction

Posted by: admin May 14, 2020 Leave a comment


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[])
   public void onDraw(Canvas canvas)
      canvas.drawText("Test",50, 50, paint);
How to&Answers:

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.rotate(90f, 50, 50);
canvas.drawText("Text",50, 50, paint);

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.

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;


Correct version is :
Canvas canvas_front = new Canvas(bitmap_front);

    Paint paint = new Paint();
    paint.setColor(Color.rgb(140, 0, 0));

 canvas_front.drawLine(0, (float) (frontIV.getHeight() * 0.9),frontIV.getWidth(), (float) (frontIV.getHeight() * 0.9), paint);

    canvas_front.rotate((float)  90 , 50, 50);
    canvas_front.drawText("Text",50, 50, paint);