Kostya  Khuta Kostya Khuta - 1 year ago 100
Android Question

canvas drawtext direction

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

Answer Source

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((float) (90, 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;