編輯:關於 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 裡那麼多手勢也缺乏介紹。經過反覆嘗試,我從實務角度為它們做了如下定義:
除此之外,我也歸納出幾點實務經驗供大家參考:
以上就是 Android 手勢操作的整理,後續會繼續補充相關知識,感謝大家對本站的支持!
淺談 Android 中加載高清大圖及圖片壓縮方式(二)
在 Android 中直接加載高清原圖往往會帶來兩個問題:一是解碼和顯示耗時較長,影響用戶體驗;二是大尺寸位圖會佔用大量內存,容易引發內存溢出甚至崩潰。因此,加載大圖時通
靠譜助手安卓模擬器黑屏、閃退怎麼辦
靠譜助手出現黑屏或閃退,最可能的原因在於顯示卡驅動。顯卡驅動的相容性對安卓模擬器向來是老大難,各家模擬器都在想辦法繞過,無奈顯卡型號多如繁星,驅動版本更是眼花撩亂,所以這
Android 動畫之二:View Animation(補間動畫)完整講解
上一篇《Android 動畫之一:Drawable Animation》講了逐幀動畫——依次顯示一系列圖片,最典型的用途是加載進度動畫。本篇接着往下走,談談 View A
Android 圖像處理入門:從相冊選圖並一鍵分享
說到圖像處理,第一步永遠是先把圖片拿到手。而在 Android 中,從手機相冊裡挑一張圖其實非常簡單。 一、打開相冊選圖 整體思路分四步: 佈局中加一個 Button