Answer the question
In order to leave comments, you need to log in
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);
canvas.drawText("Text", 0, 0, new Paint());
Answer the question
In order to leave comments, you need to log in
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question