Coding the LiveDrawingActivity class

Let's get started with coding the Activity-based class. We called this class LiveDrawingActivity, and it was auto-generated for us when we created the project.

Add the first part of the code for the LiveDrawingActivity class:

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;

public class LiveDrawingActivity extends Activity {

    private LiveDrawingView mLiveDrawingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        mLiveDrawingView = new LiveDrawingView(
        this, size.x, size.y);
        setContentView(mLiveDrawingView);
    }

}

The preceding code shows a number of errors, and we will talk about them shortly.

The code gets the number of pixels (wide and high) for the device in the following way. Take another look at the first new line of code:

Display display = getWindowManager().getDefaultDisplay();

Here, we create an object of the Display type called display and initialize it with the result of calling both the getWindowManager and getDefaultDisplay methods in turn, which are part of the Activity class.

Then, we create a new object called size of the Point type. We send size as an argument to the display.getSize method. The Point type has an x and y member variable and, therefore, so does the size object, which, after the third line of code, now holds the width and height (in pixels) of the display.

Now, we have the screen resolution in the x and y variables hidden away in the size object.

The next new thing is that we are declaring an instance of our LiveDrawingView class. Currently, this is an empty class:

private LiveDrawingView mLiveDrawingView;

Next, in onCreate, we initialize mLiveDrawingView as follows:

mLiveDrawingView = new LiveDrawingView(this, size.x, size.y);

What we are doing is passing three arguments to the LiveDrawingView constructor. We have obviously not coded a constructor yet, and as we already know, the default constructor takes zero arguments. Therefore, this line will cause an error until we fix this.

The arguments passed in are interesting. First, there's this, which is a reference to LiveDrawingActivity. The LiveDrawingView class will need to perform actions (use methods) that it needs this reference for.

The second and third arguments are the horizontal and vertical screen resolution. It makes sense that our app will need these to perform tasks, such as detecting the edge of the screen and scaling the other drawing objects to an appropriate size. We will discuss these arguments further when we code the LiveDrawingView constructor.

Next, take a look at the even stranger line that follows:

setContentView(mLiveDrawingView);

This is where, in the Canvas Demo app, we set the ImageView as the content for the app. Remember that the Activity class's setContentView method must take a View object and an ImageView is a View. This previous line of code seems to be suggesting that we will use our LiveDrawingView class as the visible content for the app. However, LiveDrawingView, despite its name, isn't a View. At least not yet.

We will fix the constructor and the not-a-View problem after we add a few more lines of code to LiveDrawingActivity.

Note

Reader challenge

Can you guess which OOP concept the solution might be?

Add the following two overridden methods, and then we will talk about them. Add them below the closing curly brace of onCreate, but before the closing curly brace of LiveDrawingActivity:

@Override
protected void onResume() {
   super.onResume();

   // More code here later in the chapter
}

@Override
protected void onPause() {
   super.onPause();

   // More code here later in the chapter
}

What we have done is overridden two more of the methods in the Activity class. We will see why we need to do this and what we will do inside these methods. The point to note here is that by adding these overridden methods, we are giving the OS the opportunity to notify us of the player's intentions in two more situations, much like we did when saving and loading our data in the Note to Self app.

It makes sense at this point to move on to the LiveDrawingView class, which is the main class of this app. We will come back to LiveDrawingActivity near the end of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.56.28