android developed an example of left and right gesture recognition for activity


The development of android adds left and right gesture recognition for activity, such as right sliding to close the current page.

/*
 *  for Left and right hand gestures
 *  1. Copy the following to the target Activity
 *  2. The target Activity the onCreate() call initGesture()
 *  3. The target Activity Need to be implements OnTouchListener, OnGestureListener
 */
   private GestureDetector mGestureDetector;
   private int verticalMinDistance = 180;
   private int minVelocity         = 0;
   private void initGesture() {
    mGestureDetector = new GestureDetector((OnGestureListener) this);
   }
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
       if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
           //  switch Activity
           // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
           // startActivity(intent);
           //Toast.makeText(this, " The left hand gestures ", Toast.LENGTH_SHORT).show();
       } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
           //  switch Activity
           // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
           // startActivity(intent);
        //Toast.makeText(this, " To the right hand the potential ", Toast.LENGTH_SHORT).show();
           finish();
           overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
       }
       return false;
   }

@Override
public void onLongPress(MotionEvent arg0) {
 // TODO Auto-generated method stub

}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
  float arg3) {
 // TODO Auto-generated method stub
 return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
 // TODO Auto-generated method stub

}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
 // TODO Auto-generated method stub
 return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
 // TODO Auto-generated method stub
 return mGestureDetector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent arg0) {
 // TODO Auto-generated method stub
 return false;
}
@Override 
public boolean dispatchTouchEvent(MotionEvent ev) {
 mGestureDetector.onTouchEvent(ev);
 return super.dispatchTouchEvent(ev);
  } 

push_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
        android:duration="500" />
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
        android:duration="500"/>
</set>

push_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="500" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.1"
        android:duration="500"/>
</set>