Using the Canvas class
Let's look at the code and the different stages required to get drawing, and then we can quickly move on to drawing something for real, with the Canvas
demo app.
Preparing the instances of the required classes
The first step is to declare the instances of the classes we require:
// Here are all the objects(instances) // of classes that we need to do some drawing ImageView myImageView; Bitmap myBlankBitmap; Canvas myCanvas; Paint myPaint;
The previous code declares references of the ImageView
, Bitmap
, Canvas
, and Paint
types. They are named myImageView
, myBlankBitmap
, myCanvas
, and myPaint
, respectively.
Initializing the objects
Next, we need to initialize our new objects before using them:
// Initialize all the objects ready for drawing // We will do this inside the onCreate method int widthInPixels = 800; int heightInPixels = 800; myBlankBitmap = Bitmap.createBitmap(widthInPixels, ...