Why is my Canvas.drawText () call just not working

Hello everyone: I am writing a class that inherits from TextView and overriding its onDraw() method, but in the method my call to canvas.drawText() does not work, the code is as below:

 protected void onDraw(Canvas canvas) { // super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(android.graphics.Color.WHITE); paint.setTextSize(20); String text = "hello"; canvas.drawText(text, 0, 0, paint); } 
+6
android textview android-canvas ondraw
source share
2 answers

This does not draw anything, because the text coordinates are lower left. Since you are trying to paint at 0.0, it will paint over the screen.

Try changing the last line to:

 canvas.drawText(text, 0, 20, paint); 
+18
source share

Great offers with everyone, great work guys. Next time, although it would be nice if you asked the guy for a comment or something, regardless of whether he tried it completely before posting it as an answer. Do you really think that the second one he got to the point that he didnโ€™t work, he just came straight to โ€œStack Overflowโ€ without experimenting?

I have an alternative proposal, which, crazy, is based on the whole question, and not on the part that could be answered without significant knowledge.

I would recommend trying your drawText call to Canvas, which is not a subclass of TextView, since it will not be overridden by the several hundred lines of code in the TextView that control it.

-one
source share

All Articles