Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw text on canvas?

i'm trying to develop a simple pie chart class for android. For now, it can take a map of labels and values and draw the pie chart. I'm yet to add the legends for the pie, which is where i need to place the texts near small rectangles on the screen corner. Any help appreciated, since i'm new to Android dev.

like image 343
Karthick Avatar asked Oct 28 '10 04:10

Karthick


People also ask

How do you draw text on 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)

Which of the following method is use to write text on the canvas?

There are two methods fillText() and strokeText() to draw text on canvas.

Can you use text on canvas?

Visuals make sense, but using text in canvas prints can be a powerful way to make a statement. Text is a strong design choice, especially used cleverly and combined with affordable, high-quality canvas prints.

How do you add text in canvas elements?

To add a text to the HTML <canvas> element, you need to use the fillText() or strokeText() methods, or you can use the combination of the two. You can also give color to your text, define the font and size, and even add gradients.


2 Answers

You will have to use the drawText method of the Canvas class.

Paint paint = new Paint();  canvas.drawPaint(paint);  paint.setColor(Color.BLACK);  paint.setTextSize(16);  canvas.drawText("My Text", x, y, paint);  

Here's the relevant documentation about it:

http://developer.android.com/reference/android/graphics/Canvas.html#drawText(java.lang.String, float, float, android.graphics.Paint)

like image 186
Jean-Philippe Jodoin Avatar answered Oct 20 '22 11:10

Jean-Philippe Jodoin


There used to be another answer here that got deleted because it was a link only. The original link is here. The code is basically the same, but I took out the non text drawing portions and also scaled up the sizes to work better on modern screen densities.

This just shows a few things you can do with text drawing.

enter image description here

Here is the updated code:

public class MainActivity extends AppCompatActivity {      DemoView demoview;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         demoview = new DemoView(this);         setContentView(demoview);     }      private class DemoView extends View {         public DemoView(Context context){             super(context);         }          @Override protected void onDraw(Canvas canvas) {             super.onDraw(canvas);              // custom drawing code here             // remember: y increases from top to bottom             // x increases from left to right             int x = 0;             int y = 0;             Paint paint = new Paint();             paint.setStyle(Paint.Style.FILL);              canvas.save();             canvas.translate(100, 200);              // make the entire canvas white             canvas.drawColor(Color.WHITE);              // draw some text using STROKE style             paint.setStyle(Paint.Style.STROKE);             paint.setStrokeWidth(1);             paint.setColor(Color.MAGENTA);             paint.setTextSize(100);             canvas.drawText("Style.STROKE", 0, 0, paint);              canvas.translate(0, 200);              // draw some text using FILL style             paint.setStyle(Paint.Style.FILL);             //turn antialiasing on             paint.setAntiAlias(true);             //paint.setTextSize(30);             canvas.drawText("Style.FILL", 0, 0, paint);              canvas.translate(0, 200);              // draw some rotated text             // get text width and height             // set desired drawing location             x = 75;             y = 185;             paint.setColor(Color.GRAY);             //paint.setTextSize(25);             String str2rotate = "Rotated!";              // draw bounding rect before rotating text             Rect rect = new Rect();             paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect);             canvas.translate(x, y);             paint.setStyle(Paint.Style.FILL);             // draw unrotated text             canvas.drawText("!Rotated", 0, 0, paint);             paint.setStyle(Paint.Style.STROKE);             canvas.drawRect(rect, paint);             // undo the translate             canvas.translate(-x, -y);              // rotate the canvas on center of the text to draw             canvas.rotate(-45, x + rect.exactCenterX(),                     y + rect.exactCenterY());             // draw the rotated text             paint.setStyle(Paint.Style.FILL);             canvas.drawText(str2rotate, x, y, paint);              //undo the translation and rotation             canvas.restore();         }     } } 

Something else that I want to try later is drawing text along a path.

See also this fuller answer here that gives the following image.

enter image description here

like image 43
Suragch Avatar answered Oct 20 '22 12:10

Suragch