Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android使用gesturedetector手勢識別示例分享

android使用gesturedetector手勢識別示例分享

編輯:關於Android編程

復制代碼 代碼如下:
public class MyGestureLintener extends SimpleOnGestureListener {
private Context context;
public MyGestureLintener(Context context) {
    super();
    this.context = context;
}

// 單擊,觸摸屏按下時立刻觸發
/*@Override
public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "Down " + e.getAction(), Toast.LENGTH_SHORT)
        .show();
    return true;
}*/
// 雙擊,手指在觸摸屏上迅速點擊第二下時觸發
@Override
public boolean onDoubleTap(MotionEvent e) {
    // TODO Auto-generated method stub
    return super.onDoubleTap(e);
}

// 雙擊的按下跟抬起各觸發一次
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    // TODO Auto-generated method stub
    return super.onDoubleTapEvent(e);
}

 

// 滑動,觸摸屏按下後快速移動並抬起,會先觸發滾動手勢,跟著觸發一個滑動手勢
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    return super.onFling(e1, e2, velocityX, velocityY);
}

// 長按,觸摸屏按下後既不抬起也不移動,過一段時間後觸發
@Override
public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "LONG " + e.getAction(), Toast.LENGTH_SHORT)
            .show();
}

// 滾動,觸摸屏按下後移動
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    Toast.makeText(context, "onScroll " + e2.getAction(), Toast.LENGTH_SHORT)
    .show();
    return true;
}

// 短按,觸摸屏按下後片刻後抬起,會觸發這個手勢,如果迅速抬起則不會
@Override
public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "Show " + e.getAction(), Toast.LENGTH_SHORT)
            .show();

}

// 單擊確認,即很快的按下並抬起,但並不連續點擊第二下
/*@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "onSingleTapConfirmed " + e.getAction(), Toast.LENGTH_SHORT)
    .show();
    return true;
}*/

// 抬起,手指離開觸摸屏時觸發(長按、滾動、滑動時,不會觸發這個手勢)
/*@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;//手勢對象
private MyGestureLintener myGestureLintener;//手勢監聽的接口對象

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myGestureLintener = new MyGestureLintener(this);

    //手勢對象的構造方法
    mGestureDetector = new GestureDetector(this,
            myGestureLintener);
}

/**GestureDetector類的onTouchEvent方法用來辨別不同的手勢*/
@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;
}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved