Adding Touch Support to Our Activity

We’ll begin by hooking into Android’s touch event system. The first thing we’ll need to do is hold onto a reference to our renderer, so let’s open up AirHockeyActivity and modify the call to setRenderer as follows:

AirHockeyTouch/src/com/airhockey/android/AirHockeyActivity.java
 
final​ AirHockeyRenderer airHockeyRenderer = ​new​ AirHockeyRenderer(this);
 
 
if​ (supportsEs2) {
 
// ...
 
glSurfaceView.setRenderer(airHockeyRenderer);

We’ll refer to this reference in our touch handler to inform the renderer of new touch events.

Listening to Touch Events

Let’s start writing our touch handler by adding the following code just before the call to setContentView:

AirHockeyTouch/src/com/airhockey/android/AirHockeyActivity.java ...

Get OpenGL ES 2 for Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.