Android手勢操作簡單實例講解
上一篇介紹的onTouch提供的事件還是相對較簡單,如果需要處理一些復(fù)雜的手勢,用這個接口就會很麻煩,因為我們要根據(jù)用戶觸摸的軌跡去判斷是什么手勢。幸好Android SDK給我們提供了GestureDetector類,通過這個類我們可以識別很多的手勢,主要是通過他的onTouchEvent(event)方法完成了不同手勢的識別。
GestureDetector這個類對外提供了兩個接口和一個外部類:
•接口:OnGestureListener,OnDoubleTapListener
•外部類:SimpleOnGestureListener
這個外部類,其實是兩個接口中所有函數(shù)的集成,它包含了這兩個接口里所有必須要實現(xiàn)的函數(shù)而且都已經(jīng)重寫,但所有方法體都是空的;不同點在于:該類是static class,程序員可以在外部繼承這個類,重寫里面的手勢處理方法。
OnGestureListener有下面的幾個動作:
•onDown(MotionEvent e):用戶按下屏幕就會觸發(fā)。
•onShowPress(MotionEvent e):如果按下的時間超過瞬間,而且在按下的時候沒有松開或者是拖動的,那么onShowPress就會執(zhí)行,具體這個瞬間是多久,我也不清楚…
•onLongPress(MotionEvent e):長按觸摸屏,超過一定時長,就會觸發(fā)這個事件。
觸發(fā)順序:onDown->onShowPress->onLongPress
•onSingleTapUp(MotionEvent e):一次單獨的輕擊抬起操作,也就是輕擊一下屏幕,立刻抬起來,才會有這個觸發(fā),當(dāng)然,如果除了Down以外還有其它操作,那就不再是Single操作了,所以也就不會觸發(fā)這個事件。
觸發(fā)順序:
點擊一下非??斓模ú换瑒樱㏕ouchup:
onDown->onSingleTapUp->onSingleTapConfirmed
點擊一下稍微慢點的(不滑動)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
•onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) :滑屏,用戶按下觸摸屏、快速移動后松開,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE, 1個ACTION_UP觸發(fā)。
參數(shù)解釋:
e1:第1個ACTION_DOWN MotionEvent
e2:最后一個ACTION_MOVE MotionEvent
velocityX:X軸上的移動速度,像素/秒
velocityY:Y軸上的移動速度,像素/秒
•onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖動事件。無論是用手拖動view,或者是以拋的動作滾動,都會多次觸發(fā),這個方法在ACTION_MOVE動作發(fā)生時就會觸發(fā)。
OnDoubleTapListener有下面的幾個動作:
•onSingleTapConfirmed(MotionEvent e):單擊事件。用來判定該次點擊是SingleTap而不是DoubleTap,如果連續(xù)點擊兩次就是DoubleTap手勢,如果只點擊一次,系統(tǒng)等待一段時間后沒有收到第二次點擊則判定該次點擊為SingleTap而不是DoubleTap,然后觸發(fā)SingleTapConfirmed事件。
觸發(fā)順序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed
•onDoubleTap(MotionEvent e):雙擊事件。
•onDoubleTapEvent(MotionEvent e):雙擊間隔中發(fā)生的動作。指觸發(fā)onDoubleTap以后,在雙擊之間發(fā)生的其它動作,包含down、up和move事件。
關(guān)于onSingleTapConfirmed和onSingleTapUp的區(qū)別:onSingleTapUp,只要手抬起就會執(zhí)行,而對于onSingleTapConfirmed來說,如果雙擊的話,則onSingleTapConfirmed不會執(zhí)行。
使用Gesture
•使用GestureDetector.OnGestureListener接口
要使用OnGestureListener接口,大致有幾步要走:
1、創(chuàng)建OnGestureListener監(jiān)聽函數(shù):
private class gestureListener implements GestureDetector.OnGestureListener{
}
2、創(chuàng)建GestureDetector實例:
構(gòu)造函數(shù)有下面三個,根據(jù)需要選擇:
GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);
GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);
GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);
注:GestureDetector現(xiàn)在已經(jīng)是deprecation狀態(tài),現(xiàn)在推薦GestureDetectorCompat
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener);
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler);
3、onTouch(View v, MotionEvent event)中攔截,在onTouch()方法中,我們調(diào)用GestureDetector的onTouchEvent()方法,將捕捉到的MotionEvent交給GestureDetector來分析是否有合適的callback函數(shù)來處理用戶的手勢
public boolean onTouch(View v, MotionEvent event) {
return gestureDetectorCompat.onTouchEvent(event);
}
4、控件綁定
TextView tv = (TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
實現(xiàn)代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="50dip" android:background="#76EE00" android:text="Gesture Detector" /> </RelativeLayout>
public class MainActivity extends Activity implements OnTouchListener{
private GestureDetectorCompat mGestureDetectorCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetectorCompat = new GestureDetectorCompat(this, new gestureListener());
TextView tv = (TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
tv.setFocusable(true);
tv.setClickable(true);
tv.setLongClickable(true);
}
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetectorCompat.onTouchEvent(event);
}
private class gestureListener implements GestureDetector.OnGestureListener{
public boolean onDown(MotionEvent e) {
showlog("onDown");
Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}
public void onShowPress(MotionEvent e) {
showlog("onShowPress");
Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();
}
public boolean onSingleTapUp(MotionEvent e) {
showlog("onSingleTapUp");
Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return true;
}
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
showlog("onScroll:"+(e2.getX()-e1.getX()) +" "+distanceX);
Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show();
return true;
}
public void onLongPress(MotionEvent e) {
showlog("onLongPress");
Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show();
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
showlog("onFling");
Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show();
return true;
}
};
public void showlog(String info) {
System.out.print("GestureDetector "+info);
}
}
•使用GestureDetector.OnDoubleTapListener接口
實現(xiàn)OnDoubleTapListener接口的前提是先實現(xiàn)OnGestureListener接口,其實除了第1步,2、3、4步和上面完全一樣,不再贅述,下面看下第一步:
同時創(chuàng)建OnGestureListener和OnDoubleTapListener監(jiān)聽函數(shù):
方法一:新建一個類同時派生自O(shè)nGestureListener和OnDoubleTapListener:
}
方法二:使用GestureDetector.setOnDoubleTapListener();函數(shù)設(shè)置監(jiān)聽:
/構(gòu)建GestureDetector實例
mGestureDetector = new GestureDetector(new gestureListener());
private class gestureListener implements GestureDetector.OnGestureListener{
}
//設(shè)置雙擊監(jiān)聽器
mGestureDetector.setOnDoubleTapListener(new doubleTapListener());
private class doubleTapListener implements GestureDetector.OnDoubleTapListener{
}
注:大家可以看到無論在方法一還是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我們說過GestureDetectorCompat 的構(gòu)造函數(shù),如下:
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener);
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler);
可以看到,它的兩個構(gòu)造函數(shù)參數(shù)都必須是OnGestureListener的實例。所以要想使用OnDoubleTapListener的幾個函數(shù),就必須先實現(xiàn)OnGestureListener。
實現(xiàn)代碼:
public class MainActivity extends Activity implements OnTouchListener{
private GestureDetectorCompat mGestureDetectorCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetectorCompat = new GestureDetectorCompat(this, new gestureListener());
TextView tv = (TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
tv.setFocusable(true);
tv.setClickable(true);
tv.setLongClickable(true);
}
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetectorCompat.onTouchEvent(event);
}
private class gestureListener implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
public boolean onDown(MotionEvent e) {
showlog("onDown");
Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}
public void onShowPress(MotionEvent e) {
showlog("onShowPress");
Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();
}
public boolean onSingleTapUp(MotionEvent e) {
showlog("onSingleTapUp");
Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return true;
}
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
showlog("onScroll:"+(e2.getX()-e1.getX()) +" "+distanceX);
Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show();
return true;
}
public void onLongPress(MotionEvent e) {
showlog("onLongPress");
Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show();
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
showlog("onFling");
Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show();
return true;
}
public boolean onSingleTapConfirmed(MotionEvent e) {
showlog("onSingleTapConfirmed");
Toast.makeText(MainActivity.this, "onSingleTapConfirmed",Toast.LENGTH_LONG).show();
return true;
}
public boolean onDoubleTap(MotionEvent e) {
showlog("onDoubleTap");
Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_LONG).show();
return true;
}
public boolean onDoubleTapEvent(MotionEvent e) {
showlog("onDoubleTapEvent");
Toast.makeText(MainActivity.this, "onDoubleTapEvent", Toast.LENGTH_LONG).show();
return true;
}
public void showlog(String info) {
System.out.print("GestureDetector "+info);
}
}
•使用GestureDetector.SimpleOnGestureListener類
使用OnGestureListener和OnDoubleTapListener接口,這樣需要重載接口所有的方法,適合監(jiān)聽所有的手勢,如果我們只想監(jiān)聽某個手勢或某幾個手勢呢,這時候就可以使用SimpleOnGestureListener類了。
它與前兩個不同的是:
1、這是一個類,在它基礎(chǔ)上新建類的話,要用extends派生而不是用implements繼承!
2、OnGestureListener和OnDoubleTapListener接口里的函數(shù)都是強制必須重寫的,即使用不到也要重寫出來一個空函數(shù)但在SimpleOnGestureListener類的實例或派生類中不必如此,可以根據(jù)情況,用到哪個函數(shù)就重寫哪個函數(shù),因為SimpleOnGestureListener類本身已經(jīng)實現(xiàn)了這兩個接口的所有函數(shù),只是里面全是空的而已。
public class MainActivity extends Activity implements OnTouchListener {
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector = new GestureDetector(new simpleGestureListener());
TextView tv = (TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
tv.setFocusable(true);
tv.setClickable(true);
tv.setLongClickable(true);
}
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
private class simpleGestureListener extends GestureDetector.SimpleOnGestureListener {
/*****OnGestureListener的函數(shù)*****/
public boolean onDown(MotionEvent e) {
Log.i("MyGesture", "onDown");
Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}
public void onShowPress(MotionEvent e) {
Log.i("MyGesture", "onShowPress");
Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();
}
public boolean onSingleTapUp(MotionEvent e) {
Log.i("MyGesture", "onSingleTapUp");
Toast.makeText(MainActivity.this, "onSingleTapUp",
Toast.LENGTH_SHORT).show();
return true;
}
/*****OnDoubleTapListener的函數(shù)*****/
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i("MyGesture", "onSingleTapConfirmed");
Toast.makeText(MainActivity.this, "onSingleTapConfirmed",Toast.LENGTH_LONG).show();
return true;
}
public boolean onDoubleTap(MotionEvent e) {
Log.i("MyGesture", "onDoubleTap");
Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_LONG).show();
return true;
}
}
}
源碼下載:http://xiazai.jb51.net/201609/yuanma/GestureDetector(jb51.net).rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android ViewPager2 使用及自定義指示器視圖實現(xiàn)
這篇文章主要為大家介紹了Android ViewPager2 使用及自定義指示器視圖實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Android框架Volley之利用Imageloader和NetWorkImageView加載圖片的方法
這篇文章主要介紹了Android框架Volley之利用Imageloader和NetWorkImageView加載圖片的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05
Android自定義控件實現(xiàn)滑動開關(guān)效果
這篇文章主要為大家詳細介紹了Android自定義控件實現(xiàn)滑動開關(guān)效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
android開發(fā)教程之文本框加滾動條scrollview
EditText與TextView加上滾動條其實很簡單,只需要在文本輸入框或者文本顯示框上面加上滾動條控件即可2014-02-02
Android中imageView圖片放大縮小及旋轉(zhuǎn)功能示例代碼
這篇文章主要介紹了Android中imageView圖片放大縮小及旋轉(zhuǎn)功能示例代碼,需要的朋友可以參考下2017-08-08

