Graphics and Touch Events

Thus far we have explored how we can capture and manipulate photographic images. Of course, that isn't all that Android has to offer in terms of images. In this chapter, we'll change up a bit and look at how we can create images by drawing graphical elements and textual elements on a Canvas. Related to this, we'll explore the capabilities provided by Android for working with touchscreens. In particular we'll build a touchscreen drawing application.

We know from the last chapter that we can draw Bitmap images on a Canvas object. That's not it for the Canvas class in Android, though. It also supports vector and text drawing. We can use a Canvas object either with Bitmap objects, as we did in the last chapter, or with Views. To start, we'll use a Canvas to create or alter a Bitmap and then move on to doing some very simple Canvas-based animation using a View.

In the same manner we used previously, we can construct a Canvas object from a mutable Bitmap. To create a mutable Bitmap, a Bitmap that can be modified, we have to supply a width, a height, and a configuration. The configuration is generally a constant value defined in the Bitmap.Config class.

The following snippet of code creates a mutable Bitmap object with the dimensions of the display for the width and height and the Bitmap.Config.ARGB_8888 constant as the configuration.

Bitmap bitmap = Bitmap.createBitmap((int)^ getWindowManager().getDefaultDisplay().getWidth(), (int)^ getWindowManager().getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);

Was this article helpful?

0 0

Post a comment