User input I. Detection Common gestures (1), gestures
Refer:
Http://blog.csdn.net/qq418716640/article/details/8508973
Http://www.cnblogs.com/mengdd/p/3335508.html
Effect: one finger (all gesture events) and (partial events );
A. All gestures
Activity_main.xml
<TextView
Android: id = "@ + id/gesture"
Android: layout_width = "match_parent"
Android: layout_height = "150dp"
Android: gravity = "center"
Android: text = "Click gesture changes"/>
<TextView
Android: id = "@ + id/doubleTap"
Android: layout_width = "match_parent"
Android: layout_height = "150dp"
Android: gravity = "center"
Android: text = "double-click gesture changes"/>
MainActivity. java
Public class MainActivity extends Activity {
Private static final String LOG_TAG = "HelloGesture ";
Private GestureDetector mGestureDetector = null;
Private TextView mGestureTextView = null;
Private TextView mDoubleTapTextView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MGestureTextView = (TextView) findViewById (R. id. gesture );
MDoubleTapTextView = (TextView) findViewById (R. id. doubleTap );
// Construct the GestureDetector object and pass in the listener object
MGestureDetector = new GestureDetector (this, mOnGestureListener );
// Input the listener object by double-clicking
MGestureDetector. setOnDoubleTapListener (mDoubleTapListener );
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
// Pass the event to the gesture detection object in the onTouchEvent method. Otherwise, the callback function in the gesture listening object will not be called.
MGestureDetector. onTouchEvent (event );
Return super. onTouchEvent (event );
}
Private OnGestureListener mOnGestureListener = new OnGestureListener (){
@ Override
Public boolean onSingleTapUp (MotionEvent e ){
Log. I (LOG_TAG, "onSingleTapUp:" + e. toString ());
MGestureTextView. setText ("onSingleTapUp :");
Return false;
}
@ Override
Public void onShowPress (MotionEvent e ){
Log. I (LOG_TAG, "onShowPress:" + e. toString ());
MGestureTextView. setText ("onShowPress :");
}
@ Override
Public boolean onScroll (MotionEvent e1, MotionEvent e2,
Float distanceX, float distanceY ){
Log. I (LOG_TAG, "onScroll:" + e1.toString () + "," + e2.toString ());
MGestureTextView. setText ("onScroll ");
Return false;
}
@ Override
Public void onLongPress (MotionEvent e ){
Log. I (LOG_TAG, "onLongPress:" + e. toString ());
MGestureTextView. setText ("onLongPress :");
}
@ Override
Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,
Float velocityY ){
Log. I (LOG_TAG, "onFling:" + e1.toString () + "," + e2.toString ());
MGestureTextView. setText ("onFling ");
Return false;
}
@ Override
Public boolean onDown (MotionEvent e ){
Log. I (LOG_TAG, "onDown:" + e. toString ());
MGestureTextView. setText ("onDown :");
Return false;
}
};
private OnDoubleTapListener mDoubleTapListener = new OnDoubleTapListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i("LOG_TAG", "onSingleTapConfirmed: " + e.toString());
mDoubleTapTextView.setText("onSingleTapConfirmed: ");
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.i("LOG_TAG", "onDoubleTapEvent: " + e.toString());
mDoubleTapTextView.setText("onDoubleTapEvent: ");
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.i("LOG_TAG", "onDoubleTap: " + e.toString());
mDoubleTapTextView.setText("onDoubleTap: ");
return false;
}
};
}
B. Some gestures
If you only want to handle a few gestures, You can inherit the GestureDetector. SimpleOnGestureListener class instead of implementing
GestureDetector. OnGestureListener Interface
MainActivity. java
Public class MainActivity extends Activity {
Private GestureDetector mGestureDetector = null;
Private TextView mGestureTextView = null;
Private TextView mDoubleTapTextView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MGestureTextView = (TextView) findViewById (R. id. gesture );
MDoubleTapTextView = (TextView) findViewById (R. id. doubleTap );
// Construct the GestureDetector object and pass in the listener object
MGestureDetector = new GestureDetector (this, new MyGestureListener ());
// Input the listener object by double-clicking
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
// Pass the event to the gesture detection object in the onTouchEvent method. Otherwise, the callback function in the gesture listening object will not be called.
This. mGestureDetector. onTouchEvent (event );
Return super. onTouchEvent (event );
}
Class MyGestureListener extends GestureDetector. SimpleOnGestureListener {
Private static final String DEBUG_TAG = "Gestures ";
@ Override
Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,
Float velocityY ){
Log. d (DEBUG_TAG, "onFling:" + e1.toString () + "," + e2.toString ());
MGestureTextView. setText ("onFling ");
Return false;
}
@ Override
Public boolean onDown (MotionEvent e ){
Log. d (DEBUG_TAG, "onDown:" + e. toString ());
MGestureTextView. setText ("onDown :");
Return false;
}
}
}