Android Tutorial

(avery) #1
Android Tutorial 259

Drawing and Working with Animation

This chapter talks about the drawing and animation features built
into Android, including creating custom View classes and working
with Canvas and Paint to draw shapes and text. We also talk about
animating objects on the screen in a variety of ways.

Drawing on the Screen

In Chapter “Exploring User Interface Screen Elements,” and
Chapter “Designing User Interfaces with Layouts,” we talk about
layouts and the various View classes available in Android to make
screen design simple and efficient. Now we must think at a slightly
lower level and talk about drawing objects on the screen. With
Android, we can display images such as PNG and JPG graphics, as
well as text and primitive shapes to the screen. We can paint these
items with various colors, styles, or gradients and modify them
using standard image transforms. We can even animate objects to
give the illusion of motion.

Working with Canvases and Paints

To draw to the screen, you need a valid Canvas object. Typically we
get a valid Canvas object by extending the View class for our own
purposes and implementing the

For example, here’s a simple View subclass called ViewWithRedDot.
We override the onDraw() method to dictate what the View looks
like; in this case, it draws a red circle on a black background.
Free download pdf