The Canvas class represents a surface on which you draw. Initially canvases start off devoid of any content, like blank transparencies for an overhead projector. Methods on the Canvas class let you draw lines, rectangles, circles, or other arbitrary graphics on the surface.

In Android, the display screen is taken up by an Activity, which hosts a View, which in turn hosts a Canvas. You get an opportunity to draw on that canvas by overriding the View.onDraw() method. The only parameter to onDraw() is a canvas on which you can draw.

Here's an example activity called Graphics, which contains a view called GraphicsView:

public class Graphics extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new GraphicsView(this));

static public class GraphicsView extends View { public GraphicsView(Context context) { super(context);

@Override protected void onDraw(Canvas canvas) { // Drawing commands go here

We're going to put some drawing commands into the onDraw() method in the next section.

0 0

Post a comment