Drawing with canvas and paint
So far, we have been using the Android Studio UI designer to implement all aspects of our interface with which our users interact.
If we want smooth-moving customized graphics, then we need to move away from predefined UI widgets.
We are going to need to start looking at and designing with individual pixels and lines. Fortunately, as you might have guessed, Android has some classes to make this nice and easy for us. We will be learning how to get started with the Canvas
and Paint
classes.
To achieve this, we will learn about the coordinate system that we use to draw our pixels and lines. Then, we will look at the Paint
and Canvas
classes themselves.
The Android coordinate system
A pixel is the smallest graphical element we can manipulate using the Paint
and Canvas
classes. It is essentially a dot. If your device resolution is 1920 x 1080 like the Nexus 5 emulator, then we have 1920 pixels across the longest length of the device and 1080 across the other.
We can therefore...