Android觸摸及手勢(shì)操作GestureDetector
現(xiàn)在的智能手機(jī)不敢說百分百的都是觸摸屏,也應(yīng)該是百分之九九以上為觸摸屏了,觸摸屏為我們操作無鍵盤、無鼠標(biāo)的手機(jī)系統(tǒng)帶來了很多的便利。當(dāng)用戶觸摸屏幕時(shí)會(huì)產(chǎn)生很多的觸摸事件,down、up、move等等。View類有個(gè)View.OnTouchListener內(nèi)部接口,通過重寫他的onTouch(View v, MotionEvent event)方法,我們可以處理一些touch事件,如下:
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); } }
OnTouch提供的事件還是相對(duì)較簡(jiǎn)單,如果需要處理一些復(fù)雜的手勢(shì),用這個(gè)接口就會(huì)很麻煩,因?yàn)槲覀円鶕?jù)用戶觸摸的軌跡去判斷是什么手勢(shì)。Android sdk給我們提供了GestureDetector(Gesture:手勢(shì)Detector:識(shí)別)類,通過這個(gè)類我們可以識(shí)別很多的手勢(shì)。
public class GestureDetector extends Object java.lang.Object android.view.GestureDetector
GestureDetector屬于android.view包,android還提供了android.gesture包支持更多的手勢(shì)操作,以后我們會(huì)介紹到。官方的介紹中使用了GestureDetectorCompat處理手勢(shì)識(shí)別,為什么使用GestureDetectorCompat替換了GestureDetector呢,官方的是這樣解釋的:
GestureDetectorCompat實(shí)例化有下面兩種方法:
GestureDetector類對(duì)外提供了兩個(gè)接口:OnGestureListener,OnDoubleTapListener,還有一個(gè)內(nèi)部類SimpleOnGestureListener;SimpleOnGestureListener類是GestureDetector提供給我們的一個(gè)更方便的響應(yīng)不同手勢(shì)的類,它實(shí)現(xiàn)了上述兩個(gè)接口,該類是static class,也就是說它實(shí)際上是一個(gè)外部類,我們可以在外部繼承這個(gè)類,重寫里面的手勢(shì)處理方法。因此實(shí)現(xiàn)手勢(shì)識(shí)別有兩種方法,一種實(shí)現(xiàn)OnGestureListener接口,另一種是使用SimpleOnGestureListener類。
OnGestureListener有下面的幾個(gè)動(dòng)作:
按下(onDown): 剛剛手指接觸到觸摸屏的那一剎那,就是觸的那一下。
拋擲(onFling): 手指在觸摸屏上迅速移動(dòng),并松開的動(dòng)作。
長(zhǎng)按(onLongPress): 手指按在持續(xù)一段時(shí)間,并且沒有松開。
滾動(dòng)(onScroll): 手指在觸摸屏上滑動(dòng)。
按?。╫nShowPress): 手指按在觸摸屏上,它的時(shí)間范圍在按下起效,在長(zhǎng)按之前。
抬起(onSingleTapUp):手指離開觸摸屏的那一剎那。
使用OnGestureListener接口,這樣需要重載OnGestureListener接口所有的方法,適合監(jiān)聽所有的手勢(shì),正如官方文檔提到的“Detecing All Supported Gestures”。
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; } }
這樣會(huì)造成有些手勢(shì)動(dòng)作我們用不到,但是還要重載。SimpleOnGestureListener類的出現(xiàn)為我們解決了這個(gè)問題,如果你想“Detecting a Subset of Supported Gestures”,SimpleOnGestureListener是最好的選擇。
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; } } }
最后了我們也解釋兩個(gè)問題:
1、onTouchEvent中為什么使用了MotionEventCompat,而不直接使用MotionEvent。因?yàn)镸otionEventCompat使更多的Action適配到API 4。
2、Android的view怎么使用手勢(shì),方法如下:
View myView = findViewById(R.id.my_view); myView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // ... Respond to touch events this.mDetector.onTouchEvent(event); return super.onTouchEvent(event); } });
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android GestureDetector用戶手勢(shì)檢測(cè)實(shí)例講解
- android使用gesturedetector手勢(shì)識(shí)別示例分享
- Android GestureDetector手勢(shì)滑動(dòng)使用實(shí)例講解
- Android手勢(shì)識(shí)別器GestureDetector使用詳解
- Android自定義viewgroup可滾動(dòng)布局 GestureDetector手勢(shì)監(jiān)聽(5)
- Android自定義GestureDetector實(shí)現(xiàn)手勢(shì)ImageView
- Android GestureDetector實(shí)現(xiàn)手勢(shì)滑動(dòng)效果
- Android編程使用GestureDetector實(shí)現(xiàn)簡(jiǎn)單手勢(shì)監(jiān)聽與處理的方法
- Android使用手勢(shì)監(jiān)聽器GestureDetector遇到的不響應(yīng)問題
- Android如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)詳解
相關(guān)文章
利用源碼編譯Android系統(tǒng)的APK和可執(zhí)行命令的方法
這篇文章主要介紹了利用源碼編譯Android系統(tǒng)的APK和可執(zhí)行命令的方法,示例在Linux系統(tǒng)環(huán)境上進(jìn)行構(gòu)建,需要的朋友可以參考下2016-02-02六款值得推薦的android(安卓)開源框架簡(jiǎn)介
同事整理的android(安卓)開源框架,個(gè)個(gè)都堪稱經(jīng)典。32 個(gè)贊!2014-06-06Android中實(shí)現(xiàn)監(jiān)聽ScrollView滑動(dòng)事件
這篇文章主要介紹了Android中實(shí)現(xiàn)監(jiān)聽ScrollView滑動(dòng)事件,本文用重寫ScrollView類的方法實(shí)現(xiàn)了一些擴(kuò)展功能,需要的朋友可以參考下2015-05-05Android實(shí)現(xiàn)照片墻效果的實(shí)例代碼
Android實(shí)現(xiàn)照片墻效果的設(shè)計(jì)思路其實(shí)也非常簡(jiǎn)單,用一個(gè)GridView控件當(dāng)作“墻”,然后隨著GridView的滾動(dòng)將一張張照片貼在“墻”上,這些照片可以是手機(jī)本地中存儲(chǔ)的,也可以是從網(wǎng)上下載的2018-05-05