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.....
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.
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).
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.
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.
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With