Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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[])
{
    super(context);
    paint.setColor(Color.BLACK);
}
   @Override
   public void onDraw(Canvas canvas)
    {
      canvas.drawText("Test",50, 50, paint);
    }
like image 206
Kostya Khuta Avatar asked Jan 12 '13 14:01

Kostya Khuta


People also ask

How do you rotate a path in canvas?

To rotate anything you can use context method - rotate(). We can't rotate line like object (like in SVG), but we can - redraw canvas with new rotated line.

What is the canvas in Android?

The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).

How to draw bitmap on canvas in Android?

Use the Canvas method public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) . Set dst to the size of the rectangle you want the entire image to be scaled into. EDIT: Here's a possible implementation for drawing the bitmaps in squares across on the canvas.


1 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.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 :P
for(char c: "Text".toCharArray()) {
    canvas.drawText(c, 50, y, paint);
    y += fontHeight;
}
like image 169
Veger Avatar answered Oct 26 '22 05:10

Veger