Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw text vertically on canvas

I would like to learn how to draw vertical text on the canvas. Sorry for maybe stupid question, but I can not solve this problem. I can do so:

 if (i ==10)
      {
          this_str2 = "0.00";
      }  


              canvas.save();
              canvas.rotate(-90,190,90);
              canvas.drawText(this_str2,  x_guide +50, drawSizes[1] + drawSizes[3]  -  (i *    drawSizes[3] / 10) +20, paint);
              canvas.restore();
      }

But it is not properly displayed on the X and Y is any other solution to this problem 7

like image 490
Domos Avatar asked Feb 13 '12 14:02

Domos


People also ask

How do I rotate text in canvas?

There are two ways you can rotate text and image elements; using the rotation controls on the canvas or by entering a numeric value in the element's properties. Method 1: Using the rotation control handle. With the element selected (an image or a piece of text), click your mouse on the rotation control handle (A).

How do you draw text on canvas?

Drawing Text on the Canvas To draw text on a canvas, the most important property and methods are: font - defines the font properties for the text. fillText(text,x,y) - draws "filled" text on the canvas. strokeText(text,x,y) - draws text on the canvas (no fill)

Can canvases contain text?

Can canvases contain text? Yes of course you can write a text on canvas with ease, and you can set the font name, font size and font color. There are two method to build a text on Canvas, i.e. fillText() and strokeText().


1 Answers

Try this custom textview, I don't remember where I took if from (here in StackOverflow), if any one remembers, please post a link in the comments.

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

public class VerticalTextView extends TextView
{
    final boolean topDown;

    public VerticalTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        final int gravity = getGravity();
        if (Gravity.isVertical(gravity) && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM)
        {
            setGravity((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
            topDown = false;
        }
        else
            topDown = true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        TextPaint textPaint = getPaint();
        textPaint.setColor(getCurrentTextColor());
        textPaint.drawableState = getDrawableState();

        canvas.save();

        if (topDown)
        {
            canvas.translate(getWidth(), 0);
            canvas.rotate(90);
        }
        else
        {
            canvas.translate(0, getHeight());
            canvas.rotate(-90);
        }

        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

        getLayout().draw(canvas);
        canvas.restore();
    }
}
like image 162
Rotemmiz Avatar answered Oct 11 '22 07:10

Rotemmiz