Touch for gesture detection

So, that covers simple interactions. How about we add another gesture to allow the user to clear all the attachment points and thus remove the Andy robot from the scene. Follow along the given steps to add another touch gesture:

  1. Scroll to the following section of code:
// Set up tap listener.
gestureDetector =
new GestureDetector(
this,
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
onSingleTap(e);
return true;
}

@Override
public boolean onDown(MotionEvent e) {
return true;
}
});

surfaceView.setOnTouchListener(
new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
  1. The preceding section of code is in the onCreate method of the HelloArActivity. It first sets up gestureDetector for interpreting the selected touch events. Then, we set a listener with setOnTouchListener in order to capture touch events and send them to the gesture detector. Just remember that the listener listens for the touch, and the gesture detector interprets the type of touch. So what we want to do is capture another form of gesture from the user.
  2. Add the following code right after the highlighted section:
@Override
public boolean onDown(MotionEvent e) { return true;}
//after this section

@Override
public void onLongPress(MotionEvent e) {
onLongPressDown(e);

}
  1. That sends our event to a new method, onLongPressDown. Let's add this new method just below the other gesture handling method by adding the following code:
private void onSingleTap(MotionEvent e) {
// Queue tap if there is space. Tap is lost if queue is full.
mQueuedSingleTaps.offer(e);

} //after this block of code
private void onLongPressDown(MotionEvent e) {
mTouches.clear();
}
  1. All that happens inside onLongPressDown is the collection of anchors, anchors is cleared. By clearing the anchors, we clear the attachment points and thus any rendering of Andy.
  2. Save the file, connect your device, and run the sample. Try placing a few big Andy's around the scene. Then, use the new long press gesture to remove them.

Good, now we have a basic understanding of how we can interact with the environment. In the next section, we will cover some basics of OpenGL ES, the 3D rendering framework we are using for Android.

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

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