<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                > 編寫:[Andrwyw](https://github.com/Andrwyw) - 原文:[http://developer.android.com/training/gestures/detector.html](http://developer.android.com/training/gestures/detector.html) 當用戶把用一根或多根手指放在觸摸屏上,并且你的應用把這樣的觸摸方式解釋為特定的手勢時,“觸摸手勢”就發生了。相應地,檢測手勢也就有以下兩個階段: 1. 收集觸摸事件的相關數據。 1. 分析這些數據,看它們是否符合,你的app所支持的手勢的標準。 ### 支持庫中的類 本節課程的示例程序使用了[GestureDetectorCompat](http://developer.android.com/reference/android/support/v4/view/GestureDetectorCompat.html)和[MotionEventCompat](http://developer.android.com/reference/android/support/v4/view/MotionEventCompat.html)類。這些類都是在 [Support Library](http://developer.android.com/tools/support-library/index.html)中定義的。如果有可能的情況話,你應該使用支持庫中的類,來為運行著Android1.6及以上版本系統的設備提供兼容性功能。需要注意的一點是,[MotionEventCompat](http://developer.android.com/reference/android/support/v4/view/MotionEventCompat.html)并不是[MotionEvent](http://developer.android.com/reference/android/view/MotionEvent.html)的替代品,而是提供了一些靜態工具類函數。你可以把[MotionEvent](http://developer.android.com/reference/android/view/MotionEvent.html)對象作為參數傳遞給這些工具類函數,來獲得與觸摸事件相關的動作(action)。 ### 收集數據 當用戶把用一根或多根手指放在觸摸屏上時,接收到這些觸摸事件的View的[onTouchEvent()](http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent))函數就會被回調。對于一系列連續的觸摸事件(位置、壓力、大小、額外的一根手指等等),[onTouchEvent()](http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent))會被調用若干次,并且最終識別為一種手勢。 當用戶第一次觸摸屏幕時,手勢就開始了。其后系統會持續地追蹤用戶手指的位置,在用戶手指全都離開屏幕時,手勢結束。在整個交互期間,被分發給[函數的](http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent))[MotionEvent](http://developer.android.com/reference/android/view/MotionEvent.html)對象,提供了每次交互的詳細信息。你的app可以使用[MotionEvent](http://developer.android.com/reference/android/view/MotionEvent.html)提供的這些數據,來判斷某種特定的手勢是否發生了。 ### 為[Activity](# "An activity represents a single screen with a user interface.")或View捕獲觸摸事件 為了捕獲[Activity](# "An activity represents a single screen with a user interface.")或View中的觸摸事件,你可以重寫[onTouchEvent()](http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent))回調函數。 接下來的代碼段,使用了[getActionMasked()](http://developer.android.com/reference/android/support/v4/view/MotionEventCompat.html#getActionMasked(android.view.MotionEvent))函數,來從event參數中抽取出用戶執行的動作。它提供了一些原始的觸摸數據,你可以使用這些數據,來判斷某個特定手勢是否發生了。 ~~~ public class MainActivity extends Activity { ... // This example shows an Activity, but you would use the same approach if // you were subclassing a View. @Override public boolean onTouchEvent(MotionEvent event){ int action = MotionEventCompat.getActionMasked(event); switch(action) { case (MotionEvent.ACTION_DOWN) : Log.d(DEBUG_TAG,"Action was DOWN"); return true; case (MotionEvent.ACTION_MOVE) : Log.d(DEBUG_TAG,"Action was MOVE"); return true; case (MotionEvent.ACTION_UP) : Log.d(DEBUG_TAG,"Action was UP"); return true; case (MotionEvent.ACTION_CANCEL) : Log.d(DEBUG_TAG,"Action was CANCEL"); return true; case (MotionEvent.ACTION_OUTSIDE) : Log.d(DEBUG_TAG,"Movement occurred outside bounds " + "of current screen element"); return true; default : return super.onTouchEvent(event); } } ~~~ 然后,你可以對這些事件做些自己的處理,以判斷某個手勢是否出現了。這種是針對自定義手勢,你所需要進行地處理。然而,如果你的app僅僅需要一些常見的手勢,如雙擊,長按,快速滑動(fling)等,你可以使用[GestureDetector](http://developer.android.com/reference/android/view/GestureDetector.html)類來完成。 [GestureDetector](http://developer.android.com/reference/android/view/GestureDetector.html)可以讓你更簡單地檢測常見手勢,并且無需自行處理單個的觸摸事件。相關內容將會在下面的[Detect Gestures](#)中討論。 ### 捕獲單個view的觸摸事件 作為[onTouchEvent()](http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent))的一種替換方式,你也可以使用setOnTouchListener()函數,來把View.OnTouchListener關聯到任意的[View](http://developer.android.com/reference/android/view/View.html)上。這樣可以讓你不繼承已有的[View](http://developer.android.com/reference/android/view/View.html),也能監聽它的觸摸事件。比如: ~~~ View myView = findViewById(R.id.my_view); myView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // ... Respond to touch events return true; } }); ~~~ 創建listener對象時,對[ACTION_DOWN](http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN)事件返回false需保持警惕。如果返回false,會讓listener對象接收不到后續的[ACTION_MOVE](http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP)、[ACTION_UP](http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP)等系列事件。這是因為[ACTION_DOWN](http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN)事件是所有觸摸事件的開端。 如果你正在寫一個自定義View,你也可以像上面描述的那樣重寫[onTouchEvent()](http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent))函數。 ### 檢測手勢 Android提供了[GestureDetector](http://developer.android.com/reference/android/view/GestureDetector.html)類來檢測一般手勢。它所支持的手勢包括[onDown()](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html#onDown(android.view.MotionEvent)), [onLongPress()](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html#onLongPress(android.view.MotionEvent)),[onFling()等。你可以把](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html#onFling(android.view.MotionEvent,android.view.MotionEvent,float,float))[GestureDetector](http://developer.android.com/reference/android/view/GestureDetector.html)和上面描述的onTouchEvent()函數結合在一起使用。 ### 檢測所有支持的手勢 當你實例化一個[GestureDetectorCompat](http://developer.android.com/reference/android/support/v4/view/GestureDetectorCompat.html)對象時,需要一個實現了[GestureDetector.OnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html)接口的的對象作為參數。當某個特定的觸摸事件發生時,[GestureDetector.OnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html)就會通知用戶。為了讓你的[GestureDetector](http://developer.android.com/reference/android/view/GestureDetector.html)對象能到接收到觸摸事件,你需要重寫View或[Activity](# "An activity represents a single screen with a user interface.")的onTouchEvent()函數,并且把所有捕獲到的事件傳遞給detector對象。 接下來的代碼段中,on型的函數的返回值是true,意味著你已經處理完這個觸摸事件了。如果返回false,則會把事件沿view棧傳遞,直到觸摸事件被成功地處理了。 運行下面的代碼段,來了解你與觸摸屏交互時,動作(action)是如何觸發的,以及每個觸摸事件[MotionEvent](http://developer.android.com/reference/android/view/MotionEvent.html)中的內容。你也會意識到,一個簡單的交互會產生多少的數據。 ~~~ public class MainActivity extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{ private static final String DEBUG_TAG = "Gestures"; private GestureDetectorCompat mDetector; // Called when the activity is first created. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Instantiate the gesture detector with the // application context and an implementation of // GestureDetector.OnGestureListener mDetector = new GestureDetectorCompat(this,this); // Set the gesture detector as the double tap // listener. mDetector.setOnDoubleTapListener(this); } @Override public boolean onTouchEvent(MotionEvent event){ this.mDetector.onTouchEvent(event); // Be sure to call the superclass implementation return super.onTouchEvent(event); } @Override public boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG,"onDown: " + event.toString()); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString()); return true; } @Override public void onLongPress(MotionEvent event) { Log.d(DEBUG_TAG, "onLongPress: " + event.toString()); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString()); return true; } @Override public void onShowPress(MotionEvent event) { Log.d(DEBUG_TAG, "onShowPress: " + event.toString()); } @Override public boolean onSingleTapUp(MotionEvent event) { Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString()); return true; } @Override public boolean onDoubleTap(MotionEvent event) { Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString()); return true; } @Override public boolean onDoubleTapEvent(MotionEvent event) { Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString()); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent event) { Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString()); return true; } } ~~~ ### 檢測支持手勢的部分 如果你只想處理幾種手勢,你可以選擇繼承[GestureDetector.SimpleOnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html)類,而不是實現[GestureDetector.OnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html)接口。 GestureDetector.SimpleOnGestureListener類實現了所有的on型函數,其中都返回false。因此,你可以僅僅重寫你所需要的函數。比如,下面的代碼段中,創建了一個繼承自[GestureDetector.SimpleOnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html)的類,并重寫了onFling()和onDown()函數。 無論你是否使用[GestureDetector.OnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html)類,最好都實現onDown()函數并且返回true。這是因為所有的手勢都是由onDown()消息開始的。如果你讓onDown()函數返回false,就像[GestureDetector.SimpleOnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html)類中默認實現地那樣,系統會假定你想忽略手勢的剩余部分,[GestureDetector.OnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html)中的其他函數也就永遠不會被調用。這可能讓你的app出現意想不到的問題。僅僅當你真的想忽略整個手勢時,你才應該讓onDown()函數返回false。 ~~~ public class MainActivity extends Activity { private GestureDetectorCompat mDetector; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDetector = new GestureDetectorCompat(this, new MyGestureListener()); } @Override public boolean onTouchEvent(MotionEvent event){ this.mDetector.onTouchEvent(event); return super.onTouchEvent(event); } class MyGestureListener extends GestureDetector.SimpleOnGestureListener { private static final String DEBUG_TAG = "Gestures"; @Override public boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG,"onDown: " + event.toString()); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString()); return true; } } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看