D
D
Decker2015-10-22 01:05:33
Android
Decker, 2015-10-22 01:05:33

How to display text on canvas?

Good day to all. I'm learning to program for Android, so far, let's say, at the very beginning of the journey;) I set myself an educational task - to draw a chessboard, while the board itself and the cell in it are defined as separate classes. The board cell class inherits from Button and is defined as: The GamePiece cells themselves are displayed in a LinearLayout, for which the onLayout method is overridden, inside which new element sizes are calculated for each View GamePiece. Which gives us the ability to place arbitrary chessboards on the screen, for example 4x4, 8x8, etc. The board cell itself is drawn from the picture in onDraw(Canvas canvas):
public class GamePiece extends Button { .. }

bitmap = BitmapFactory.decodeResource(getResources(), imageId);
bitmap = Bitmap.createScaledBitmap(bitmap, edgeSize, edgeSize, true);
canvas.drawBitmap(bitmap, padding, padding, null);

Where imageId is the identifier of the image in the resources (black or white). I want to fully understand how to work with canvas. For example, I want to display text with a cell number on top of this picture. But for example, if you do this: I don't see the text. In which direction to dig and how to display text on top of BitMap? Thanks in advance.
canvas.drawText("Text", 0, 0, new Paint());

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Decker, 2015-10-22
@Decker

All ... saw ... thanks ;) I did this instead of drawing a bitmap:

Paint p = new Paint();
        p.setColor(Color.RED);
        canvas.drawRect(new Rect(0, 0, 10, 10), p);
        p.setColor(Color.YELLOW);
        canvas.drawText("*", 10, 10, p);

And I saw both red squares and yellow text. Now it remains to understand how to set the size, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question