android使用gesturedetector手勢識別樣本分享_Android

來源:互聯網
上載者:User

複製代碼 代碼如下:

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;
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.