Home > Devices > Fire Phone

MotionGesture Sample App

DropDownMenuActivity

The MotionGesture sample is a sample app that illustrates obtaining gesture events and using them in your app. The sample app consists of a single Activity, DropDownMenuActivity. This Activity displays a drop-down menu in response to a tilt gesture from the device user. In response to a second tilt gesture, the Activity hides the drop-down menu. The sequence of operations illustrates the program flow for receiving gesture events.

When the Activity is first created, it obtains a GestureManager object.

mGestureManager = GestureManager.createInstance(thisActivity);

Every time the Activity is launched, it obtains a TiltGesture object representing the type of gesture the Activity intends to listen for.

mTiltGesture = Gesture.getGestureFromId(Gesture.TILT);

The Activity uses this TiltGesture object to register as a listener for tilt events, and it specifies that the direction of the tilt should be forward.

@Override
public void onGestureEvent(GestureEvent event) {
    Log.d(TAG, "Gesture Received: " + event.toString());

    // Move drawer down if it is not already down
    if (!mDrawerDown) {
        final TranslateAnimation translate = new TranslateAnimation(0, 0,
                                                                    -mDropDownDrawer.getHeight(), 0);
        translate.setDuration(ANIMATE_DURATION);
        // Make sure the animated image persists.
        translate.setFillAfter(true);

        mDropDownDrawer.startAnimation(translate);
        mDrawerDown = true;

    // Move drawer up if it is not already up
    } else {
            final TranslateAnimation translate = new TranslateAnimation(0, 0, 0,
                                                                        -mDropDownDrawer.getHeight());
            translate.setDuration(ANIMATE_DURATION);

            mDropDownDrawer.startAnimation(translate);
            mDrawerDown = false;
        }
    }
}

When the Activity is paused or shutdown, it unregisters the listener.

@Override
public void onPause() {
    // Release the listener
    if (mGestureManager != null) {
        mGestureManager.unregisterListener(this);
    }
    super.onPause();
}