Consider your Options

When drawing 2D graphics, you'll typically do so in one of two ways:

a. Draw your graphics or animations into a View object from your layout. In this manner, the drawing (and any animation) of your graphics is handled by the system's normal View hierarchy drawing process — you simply define the graphics to go inside the View.

b. Draw your graphics directly to a Canvas. This way, you personally call the appropriate class's draw() method (passing it your Canvas), or one of the Canvas draw...() methods (like drawPicture()). In doing so, you are also in control of any animation.

Option "a," drawing to a View, is your best choice when you want to draw simple graphics that do not need to change dynamically and are not part of a performance-intensive game. For example, you should draw your graphics into a View when you want to display a static graphic or predefined animation, within an otherwise static application. Read Simple Graphics Inside a View.

Option "b," drawing to a Canvas, is better when you're application needs to regularly re-draw itself. Basically, any video game should be drawing to the Canvas on its own. However, there's more than one way to do this:

• In the same thread as your UI Activity, wherein you create a custom View component in your layout, call invalidate() and then handle the onDraw() callback..

• Or, in a separate thread, wherein you manage a SurfaceView and perform draws to the Canvas as fast as your thread is capable (you do not need to request invalidate()).

...Begin by reading Draw with a Canvas.

0 0

Post a comment