Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 手勢操作編程完全指南

編輯:關於 Android 程式設計

手勢操作為我們使用智慧型裝置帶來了截然不同的體驗。在 Android 開發中,手勢操作編程幾乎是必備技能。那麼它的原理為何?我們又該如何實作呢?

手勢操作原理

在 Android 系統中,每一次手勢互動都會依序經歷以下流程:

1. 接觸觸控螢幕的瞬間,觸發一個 MotionEvent 事件。

2. 該事件被 OnTouchListener 監聽,在其 onTouch() 方法中取得這個 MotionEvent 物件。

3. 透過 GestureDetector(手勢識別器)把該 MotionEvent 轉發給 OnGestureListener。

4. OnGestureListener 取得物件後,根據封裝的資訊做出合適的回饋。

這個順序,就是手勢操作的核心原理。

手勢操作類與接口

接下來認識 MotionEvent、GestureDetector 與 OnGestureListener:

MotionEvent:用於封裝手勢、觸控筆、軌跡球等動作事件,內部封裝了記錄橫軸與縱軸座標的 X、Y 兩個屬性。

GestureDetector:負責識別各種手勢。

OnGestureListener:手勢互動的監聽接口,提供多個抽象方法,並依 GestureDetector 的識別結果呼叫對應方法。

手勢操作實例

下面透過一個切換圖片的範例,演示手勢互動的實現,幫助大家更深刻地理解執行順序與各手勢動作的差異。

首先提供一個只含 ImageView 的佈局檔 main.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center"/> </LinearLayout> 

接著完成 Activity。由於要監聽觸控事件與手勢事件,該 Activity 必須實作 OnTouchListener 與 OnGestureListener 兩個接口,並重寫其中的方法:

 public class MainActivity extends Activity implements OnTouchListener, OnGestureListener { //創建一個用於識別收拾的GestureDetector對象waiyuwu.blogcn.com private GestureDetector detector = new GestureDetector(this); //定義一個數組,用於放漂亮的女孩 int[] girls = new int[]{R.drawable.girl1, R.drawable.girl2, R.drawable.girl3}; //定義數組下標,以方便觀看各個女孩 private int index; private ImageView image; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); image = (ImageView)findViewById(R.id.image); //設置一個初始顯示的girl吧 image.setImageResource(girls[index]); //監聽這個ImageView組件上的觸摸屏時間 image.setOnTouchListener(this); //下面兩個要記得設哦,不然就沒法處理輕觸以外的事件了,例如拋擲動作。 image.setLongClickable(true); detector.setIsLongpressEnabled(true); } //用於呼喊下一個女孩的方法 public void goNext(){ index++; index = Math.abs(index % girls.length); image.setImageResource(girls[index]); } //用戶呼喚上一個女孩的方法 public void goPrevious(){ index--; index = Math.abs(index % girls.length); image.setImageResource(girls[index]); } //重寫OnTouchListener的onTouch方法 //此方法在觸摸屏被觸摸,即發生觸摸事件(接觸和撫摸兩個事件,挺形象)的時候被調用。 @Override public boolean onTouch(View v, MotionEvent event) { detector.onTouchEvent(event); return true; } //在按下動作時被調用 @Override public boolean onDown(MotionEvent e) { return false; } //在拋擲動作時被調用 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //velocityX表示橫向的移動,根據手指移動的方向切換女孩 if(velocityX < 0){ goNext(); }else if(velocityX > 0){ goPrevious(); } return false; } //在長按時被調用 @Override public void onLongPress(MotionEvent e) { } //在滾動時調用 @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } //在按住時被調用 @Override public void onShowPress(MotionEvent e) { } //在抬起時被調用 @Override public boolean onSingleTapUp(MotionEvent e) { return false; } } 

各手勢方法的含義

初學 Android 時就覺得 Google 的文檔不太理想,研究手勢時感受更深——許多常量、屬性與方法連個說明都沒有,OnGestureListener 裡那麼多手勢也缺乏介紹。經過反覆嘗試,我從實務角度為它們做了如下定義:

  1. 按下(onDown):手指剛接觸觸控螢幕的那一瞬間。
  2. 拋擲(onFling):手指在螢幕上迅速移動後鬆開的動作。
  3. 長按(onLongPress):手指按住一段時間且未鬆開。
  4. 滾動(onScroll):手指在螢幕上滑動。
  5. 按住(onShowPress):手指按在螢幕上,時間範圍介於按下與長按之間。
  6. 抬起(onSingleTapUp):手指離開螢幕的那一瞬間。

除此之外,我也歸納出幾點實務經驗供大家參考:

  1. 任何手勢動作都會先執行一次按下(onDown)。
  2. 長按(onLongPress)之前必定會先執行一次按住(onShowPress)。
  3. 按住(onShowPress)與按下(onDown)之後,都會執行一次抬起(onSingleTapUp)。
  4. 長按(onLongPress)、滾動(onScroll)與拋擲(onFling)之後,都不會再執行抬起(onSingleTapUp)。

以上就是 Android 手勢操作的整理,後續會繼續補充相關知識,感謝大家對本站的支持!

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved