Creating Bitmaps

Let's do a little bit of theory before we dive into the code and consider exactly how we are going to bring images to life on the screen. To draw a bitmap, we will use the drawBitmap method of the Canvas class.

First, we would need to add a bitmap to the project into the res/drawable folder—we will do this for real in the Bitmap demo app coming up shortly. For now, assume the graphics file/bitmap has a name of myImage.png.

Next, we would declare an object of the Bitmap type, just the same as we did for the Bitmap we used for our background in the previous demo:

Bitmap mBitmap;

Next, we would need to initialize the mBitmap using our preferred image, which we previously added to the project's drawable folder:

mBitmap = BitmapFactory.decodeResource ...

Get Android Programming for Beginners - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.