Reputation: 11791
All, Forgive me just learning Android
Graphics, And I just knew a little about it. I can draw something like line with Paint
which hosted in the Canvas
,
Paint background = new Paint();
background.setColor(getResources().getColor(
R.color.puzzle_background));
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
Paint dark = new Paint();
dark.setColor(getResources().getColor(R.color.puzzle_dark));
Paint hilite = new Paint();
hilite.setColor(getResources().getColor(R.color.puzzle_hilite));
Paint light = new Paint();
light.setColor(getResources().getColor(R.color.puzzle_light));
// Draw the minor grid lines
for (int i = 0; i < 9; i++) {
canvas.drawLine(0, i * height, getWidth(), i * height, light);
canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(), light);
canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
hilite);
}
// Draw the major grid lines
for (int i = 0; i < 9; i++) {
if (i % 3 != 0)
continue;
canvas.drawLine(0, i * height, getWidth(), i * height, dark);
canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
hilite);
}
But I don't know What are the necessary elements if I want to draw something in the Android
.And Is the Paint
one of the necessary elements? thanks.
Upvotes: 0
Views: 73
Reputation: 5322
There are basically three ways that I know of to draw in Android.
1- Overriding onDraw
(as you did) which provides you with the canvas of the View to draw with on the View's surface. This is used for simple drawings as the code runs in the main Thread.
2- Subclass SurfaceView
, which provides a separate thread for drawing (provides a Canvas as well of a Surface that you draw on), for medium load drawing since it has a separate thread, so it can be faster than 1.
3- OpenglSurfaceView
, which is a different concept, in which you create a 3D scene and render it directly using the GPU. This is for heavy weight drawing, since talking to the GPU directly enables much faster animations, specially in 3D games.
As I said in my comment, Canvas
is your palette, which provides you with basic tools to draw lines, circles, images, ...etc. While, Paint
is your brush, which lets you determine stroke width, color, Text size and fonts, ...etc. you may not need to change Paint object if you are satisfied with the defaults of Canvas
. Your drawing board is the View's surface.
Upvotes: 1