Home » Android » java – Draw Text on Canvas at Angle

java – Draw Text on Canvas at Angle

Posted by: admin June 15, 2020 Leave a comment

Questions:

How can i draw text on canvas as shown in below image highlighted in Green rectangle .

enter image description here

I have done following code…. but from this code i can write text in straight. can’t write text at angle.

Bitmap bmpLayered = Bitmap.createBitmap(bmpMain.getWidth(), bmpMain
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas cv = new Canvas(bmpLayered);

Paint charPaint = new Paint();
        charPaint.setAntiAlias(true);
        charPaint.setStyle(Paint.Style.FILL);
        charPaint.setTextSize(24);
        charPaint.setColor(Color.BLACK);
        charPaint.setStrokeWidth(3);

cv.drawText("None", 570, 222, charPaint);

Please help me to solve this.

Thanks.

How to&Answers:
cv.save();
cv.rotate(-45, x, y);
cv.drawText("your text here", x, y, paint);
cv.restore();

where cv being reference to your canvas, x & y the point where you want to draw.

Answer:

After you’ve drawn the text to the canvas you could rotate the canvas.

cv.drawText("None", 570, 222, charPaint);
//rotate the canvas
cv.rotate(45f);
// or around a pivot point
cv.rotate(45f, 100, 100);

Android Developer: Graphics-Canvas Rotate