package com.aincc.libtest.activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; import com.aincc.lib.common.BaseActivity; import com.aincc.lib.common.annotation.InjectView; import com.aincc.lib.util.Logger; import com.aincc.libtest.R; public class GestureTest extends BaseActivity { private static final String LOG = "GESTURE"; private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; private GestureDetector gestureScanner; // @InjectView // private TextView text; @InjectView private View topView; @InjectView private View bottomView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gesture_test); mappingViews(this); initializeUI(); } @Override protected void initializeUI() { super.initializeUI(); gestureScanner = new GestureDetector(this, gestureListener); } @Override public boolean onTouchEvent(MotionEvent me) { return gestureScanner.onTouchEvent(me); } private OnGestureListener gestureListener = new OnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { Logger.d1(LOG, "onSingleTapUp " + e.getX() + ":" + e.getY()); return false; } @Override public void onShowPress(MotionEvent e) { Logger.d1(LOG, "onShowPress " + e.getX() + ":" + e.getY()); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Logger.d1(LOG, "onScroll"); return false; } @Override public void onLongPress(MotionEvent e) { Logger.d1(LOG, "onLongPress " + e.getX() + ":" + e.getY()); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Logger.d1(LOG, "onFling"); try { if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; // right to left swipe if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show(); } // left to right swipe else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show(); } // down to up swipe else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(getApplicationContext(), "Swipe up", Toast.LENGTH_SHORT).show(); } // up to down swipe else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(getApplicationContext(), "Swipe down", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { } return false; } @Override public boolean onDown(MotionEvent e) { Logger.d1(LOG, "onDown " + e.getX() + ":" + e.getY()); return false; } }; }