Book Image

Learning Java by Building Android Games

By : John Horton
Book Image

Learning Java by Building Android Games

By: John Horton

Overview of this book

Table of Contents (17 chapters)
Learning Java by Building Android Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Android Canvas demo app


Let's make an app that uses the Canvas and Paint classes and do a bit of drawing. This example will be completely static (no animation), so we can clearly see how to use Canvas and Paint without cluttering the code with things we will learn later.

In this demo app, we use some conceptually helpful variable names to help us grasp the role that each object is playing, but we will go through the whole thing at the end to make sure we know exactly what is going on at each stage. Of course, you don't have to type all of this. You can open the completed code files from the CanvasDemo folder in the Chapter7 folder of the download bundle:

  1. Start a new project and call it CanvasDemo. Tidy up the unnecessary imports and overrides if you want to.

  2. Open activity_main.xml in the editor. Drag an ImageView from the palette to the layout. The ImageView has an ID by default, which is imageView. Now we will use this ID in our code.

  3. Switch to MainActivity.java in the editor. First, we will...