The MainActivity class

The default project generated with Empty Activity also created a default MainActivity.java file. In the hierarchy pane, locate the app/java/ directory that contains a subdirectory named com.cardbookvr.skeleton.

Note

Note, this is different than the androidTest version of the directory, we're not using that one! (Your name may vary based on the actual project and domain names given when you created the project.)

In this folder, double-click on the MainActivity.java file to open it for editing. The default file looks like this:

package com.cardbookvr.skeleton;

import ...

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

The first thing you should notice is the extends AppCompatActivity class (or ActionBarActivity) for the built-in Android action bar. We do not need this. We will rather define the activity to the extends CardboardActivity and implements the CardboardView.StereoRenderer interfaces. Modify the class declaration line of code, as follows:

public class MainActivity extends CardboardActivity implements CardboardView.StereoRenderer {

As this is a Google Cardboard application, we need to define the MainActivity class as a child class of the CardboardActivity class given by the SDK. We do this using the extends keyword.

MainActivity needs to also implement, at a minimum, the stereo renderer interface defined as CardboardView.StereoRender. We do this using the implements keyword.

One of the nice things about Android Studio is how it does work for you as you write the code. When you enter extends CardboardActivity, the IDE automatically adds an import statement for the CardboardActivity class at the top of the file. When you enter implements CardboardView.StereoRenderer, it adds an import statement to the CardboardView class.

As we continue to add code, Android Studio will identify when we need additional import statements and automatically add them for us. Therefore, I won't bother to show you the import statements in the code that follows. On occasion it may find the wrong one when, for example, there's multiple Camera or Matrix classes among your libraries, and you'll need to resolve it to the correct reference.

We'll now fill in the body of the MainActivity class with stubs for the functions that we're going to need. The CardboardView.StereoRenderer interface that we're using defines a number of abstract methods that we can override, as documented in the Android API Reference for the interface (refer to https://developers.google.com/cardboard/android/latest/reference/com/google/vrtoolkit/cardboard/CardboardView.StereoRenderer).

This is quickly accomplished in Studio in a number of ways. Either use the intellisense context menu (the light bulb icon) or go to Code | Implement Methods… (or Ctrl + I). By placing your cursor at the red error underline and pressing Alt + Enter, you will also be able to accomplish the same goal. Do it now. You will be asked to confirm the methods to implement, as shown in the following screenshot:

The MainActivity class

Ensure that all are selected and click on OK.

Stubs for the following methods will be added to the MainActivity class:

  • onSurfaceCreated: This is called when the surface is created or recreated. It should create buffers and variables needed to display graphics.
  • onNewFrame: This is called when a new frame is about to be drawn. It should update the application data that changes from one frame to the next, such as animations.
  • onDrawEye: This renders the scene for one eye for the current camera viewpoint (called twice per frame, unless you have three eyes!).
  • onFinishFrame: This is called before a frame is finished.
  • onRenderShutdown: This is called when the renderer thread is shutting down (rarely used).
  • onSurfaceChanged: This is called when there is a change in the surface dimensions (for example, when a portrait/landscape rotation is detected).

I've listed these methods in an order that mirrors the life cycle of a Cardboard Android application.

The @Override directive means that these functions are originally defined in the CardboardView.StereoRenderer interface and we're replacing (overriding) them in our MainActivity class here.

Default onCreate

All Android activities expose an onCreate() method that is called when the activity is first created. This is where you should do all your normal static setups and bindings. The stereo renderer interface and Cardboard activity class are the foundations of the Cardboard SDK.

The default onCreate method makes a standard onCreate call to the parent activity. Then, it registers the activity_main layout as the current content view.

Edit onCreate() by adding the CardboadView instance, as follows:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CardboardView cardboardView = (CardboardView) findViewById(R.id.cardboard_view);
        cardboardView.setRenderer(this);
        setCardboardView(cardboardView);
    }

To set up the CardboardView instance for the app, we get its instance by looking it up by the resource ID given in activity_main.xml and then set it up with a couple of function calls.

This object is going to do the stereoscopic rendering to the display, so we call setRenderer(this) to specify it as the receiver of the StereoRenderer interface methods.

Note

Note that your activity doesn't have to implement the interface. You can have any class define these methods, such as an abstracted renderer as we'll see later in this book.

Then we associate the CardboardView class with this activity by calling setCardboardView(cardboardView) so that we'll be able to receive any required life cycle notifications, including the StereoRenderer interface methods, such as onSurfaceCreated and onDrawEye.

Building and running

Let's build and run it:

  1. Go to Run | Run 'app', or simply use the green-triangle Run icon on the toolbar.
  2. If you've made changes, Gradle will do its build thing.
  3. Select the Gradle Console tab at the bottom of the Android Studio window to view the Gradle build messages. Then, assuming that all goes well, the APK will be installed on your connected phone (it's connected and turned on, right?).
  4. Select the Run tab at the bottom to view the upload and launch messages.

You shouldn't get any build errors. But of course, the app doesn't actually do anything or draw anything on the screen. Well, that's not entirely true! The Cardboard SDK, via CardboardView.StereoRenderer, provides a stereoscopic split screen with a vertical line in between and a gear icon, as shown in the following screenshot:

Building and running

The vertical line will be used to align your phone properly on the Cardboard viewer device.

The gear icon opens the standard configuration settings utility which includes the ability to scan a QR code to configure the SDK for the lenses and other physical attributes of your specific device (as explained in Chapter 1, Virtual Reality for Everyone, in the Configuring your Cardboard viewer section).

Now, we've built a skeleton Google Cardboard app for Android. You'll follow similar steps to start each project in this book.

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

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