手勢檢測GestureDetector,gesturedetector

來源:互聯網
上載者:User

手勢檢測GestureDetector,gesturedetector

由於在onTouchEvent中判斷使用者手勢的真實想法很不容易,因此Android提供了GestureDetector檢測器來協助我們識別手勢。藉助於GestureDetector,可以在大多數場合下辨別出常用的幾個手勢事件,如點擊、長按、翻頁等等。下面是GestureDetector的相關方法:

建構函式 : GestureDetector(Context context, OnGestureListener listener)
監聽器類名 : OnGestureListener
設定監聽器的方法,先給指定控制項註冊觸摸監聽器,然後在觸摸方法onTouch中由GestureDetector接管觸摸事件 : 

private ScrollTextView tv_rough;  private GestureDetector mGesture;        tv_rough = (ScrollTextView) findViewById(R.id.tv_rough);      tv_rough.setOnTouchListener(this);      mGesture = new GestureDetector(this, this);        @Override  public boolean onTouch(View v, MotionEvent event) {      return mGesture.onTouchEvent(event);  }  

另外,也可在當前視圖或當前Activity中重寫onTouchEvent方法,在該方法中由GestureDetector接管觸摸事件。

監聽器需要重寫的方法 : 
onDown : 在使用者按下時調用
onShowPress : 已按下但還未滑動或鬆開時調用,通常用於pressed狀態時的高亮顯示
onSingleTapUp : 在使用者輕點一下再彈起時調用,通常用於點擊事件
onScroll : 在使用者滑動過程中調用
onLongPress : 在使用者長按時調用,通常用於長按事件
onFling : 在使用者飛快掠出一段距離時調用,通常用於翻頁事件

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.