Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

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 :Pfor(char c: "Text".toCharArray()) {
    canvas.drawText(c, 50, y, paint);
    y += fontHeight;
}

Solution 2:

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

Paintpaint=newPaint();
    paint.setColor(Color.rgb(140, 0, 0));
    paint.setAlpha(80);
    paint.setStrokeWidth(2);

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

    canvas_front.save();
    canvas_front.rotate((float)  90 , 50, 50);
    canvas_front.drawText("Text",50, 50, paint);
    canvas_front.restore();
    frontIV.setImageBitmap(bitmap_front);

Post a Comment for "Canvas Drawtext Direction"