Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Canvas.drawText

I have a view, I'm drawing with the Canvas object in the onDraw(Canvas canvas) method. My code is:

Paint paint = new Paint(); paint.setColor(Color.WHITE); paint.setStyle(Style.FILL); canvas.drawPaint(paint);  paint.setColor(android.R.color.black); paint.setTextSize(20); canvas.drawText("Some Text", 10, 25, paint); 

The problem is the text doesn't show through the background, what am I doing wrong? If I remove the canvas.drawPaint(paint) and paint.setColor(android.R.color.black) you can see the text on the screen.....

like image 373
Gaz Avatar asked Apr 16 '10 18:04

Gaz


People also ask

How to draw text in canvas Android?

This article explains how to draw text inside a rectangle using the canvas in Android. Android Studio is used for the sample. In this, you will first create a paint object. Then call the setColor() method of paint to set the color of the rectangle by ing a color variable.

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 write text on canvas in Android studio?

Another (arguably better) way to draw text on a canvas is to use a StaticLayout . This handles multiline text when needed. String text = "This is some text."; TextPaint textPaint = new TextPaint(); textPaint. setAntiAlias(true); textPaint.

How to draw bitmap on canvas in Android?

Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix. Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle. Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle.


1 Answers

Worked this out, turns out that android.R.color.black is not the same as Color.BLACK. Changed the code to:

Paint paint = new Paint();  paint.setColor(Color.WHITE);  paint.setStyle(Style.FILL);  canvas.drawPaint(paint);   paint.setColor(Color.BLACK);  paint.setTextSize(20);  canvas.drawText("Some Text", 10, 25, paint);  

and it all works fine now!!

like image 179
Gaz Avatar answered Oct 13 '22 06:10

Gaz