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實(shí)現(xiàn)一個(gè)帶粘連效果的LoadingBar
Loading效果相信大家應(yīng)該都實(shí)現(xiàn)過,最近發(fā)現(xiàn)了一個(gè)不錯(cuò)的效果,決定分享給大家,所以下面這篇文章主要給大家介紹了關(guān)于利用Android實(shí)現(xiàn)一個(gè)帶粘連效果的LoadingBar的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-12-12
Android觸摸事件如何實(shí)現(xiàn)筆觸畫布詳解
這篇文章主要給大家介紹了關(guān)于Android觸摸事件如何實(shí)現(xiàn)筆觸畫布的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
Android 實(shí)現(xiàn)抖音頭像底部彈框效果的實(shí)例代碼
這篇文章主要介紹了Android 實(shí)現(xiàn)抖音頭像底部彈框效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android?Studio實(shí)現(xiàn)簡(jiǎn)單補(bǔ)間動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡(jiǎn)單補(bǔ)間動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Android 文件數(shù)據(jù)存儲(chǔ)實(shí)例詳解
這篇文章主要介紹了Android 文件數(shù)據(jù)存儲(chǔ)實(shí)例詳解的相關(guān)資料,這里附有實(shí)例代碼,幫助大家學(xué)習(xí)理解,需要的朋友可以參考下2016-12-12
android ViewPager實(shí)現(xiàn)自動(dòng)無限輪播和下方向?qū)A點(diǎn)
本篇文章主要介紹了android ViewPager實(shí)現(xiàn)自動(dòng)輪播和下方向?qū)A點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
Android開發(fā)之超強(qiáng)圖片工具類BitmapUtil完整實(shí)例
這篇文章主要介紹了Android開發(fā)之超強(qiáng)圖片工具類BitmapUtil,結(jié)合完整實(shí)例形式分析了Android圖片的常用操作技巧,包括圖片的加載、轉(zhuǎn)換、縮放、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android 自定義通用的loadingview實(shí)現(xiàn)代碼
本篇文章主要介紹了Android 自定義通用的loadingview實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01

