android USES gesturedetector gesture recognition examples to share

  • 2020-05-24 06:05:52
  • OfStack


public class MyGestureLintener extends SimpleOnGestureListener {
private Context context;
public MyGestureLintener(Context context) {
    super();
    this.context = context;
}
//  Click, the touch screen will trigger immediately when pressed 
/*@Override
public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "Down " + e.getAction(), Toast.LENGTH_SHORT)
        .show();
    return true;
}*/
//  Double - click, finger on the touch screen quickly click 2 When the trigger 
@Override
public boolean onDoubleTap(MotionEvent e) {
    // TODO Auto-generated method stub
    return super.onDoubleTap(e);
}
//  Double-click the press down and lift each trigger 1 time 
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    // TODO Auto-generated method stub
    return super.onDoubleTapEvent(e);
}
 
//  Sliding, pressing the touch screen and then moving quickly and lifting, will first trigger the rolling gesture, followed by the trigger 1 A sliding gesture 
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    return super.onFling(e1, e2, velocityX, velocityY);
}
//  Long press, touch screen press after neither lifting nor moving, over 1 Triggered after a period of time 
@Override
public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "LONG " + e.getAction(), Toast.LENGTH_SHORT)
            .show();
}
//  Scroll, touch screen press and move 
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    Toast.makeText(context, "onScroll " + e2.getAction(), Toast.LENGTH_SHORT)
    .show();
    return true;
}
//  A short press, which is held down by the touch screen for a moment and then raised, triggers the gesture, but not if it is raised quickly 
@Override
public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "Show " + e.getAction(), Toast.LENGTH_SHORT)
            .show();
}
//  Click ok, and press down and up quickly, but not continuously click first 2 Under the 
/*@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "onSingleTapConfirmed " + e.getAction(), Toast.LENGTH_SHORT)
    .show();
    return true;
}*/
//  Lift and trigger when finger leaves touch screen ( Long presses, rolls, and slides will not trigger this gesture )
/*@Override
public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "onSingleTapUp " + e.getAction(), Toast.LENGTH_SHORT)
    .show();
    return true;
}*/
public class MainActivity extends Activity {
private GestureDetector mGestureDetector;// Gestures object 
private MyGestureLintener myGestureLintener;// Interface object for gesture listening 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myGestureLintener = new MyGestureLintener(this);
    // The construction of gesture objects 
    mGestureDetector = new GestureDetector(this,
            myGestureLintener);
}
/**GestureDetector Of the class onTouchEvent Methods are used to identify different gestures */
@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean b = false;
    int i = event.getAction();
    int j = MotionEvent.ACTION_MOVE;
    System.out.println(i+"<----------------->"+j);
    b = mGestureDetector.onTouchEvent(event);
    if (b) {
        Intent in = new Intent();
        in.setClass(this, testActivity.class);
        startActivity(in);
    }
    return b;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}


Related articles: