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 (getResources(), R.drawable.myImage);
The static decodeResource
method of the BitmapFactory
method is used to initialize mBitmap
. It takes two parameters. The first is a call to getResources
, which is...