国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android手勢操作簡單實例講解

2019-12-12 05:24:04
字體:
來源:轉載
供稿:網友

上一篇介紹的onTouch提供的事件還是相對較簡單,如果需要處理一些復雜的手勢,用這個接口就會很麻煩,因為我們要根據用戶觸摸的軌跡去判斷是什么手勢。幸好Android SDK給我們提供了GestureDetector類,通過這個類我們可以識別很多的手勢,主要是通過他的onTouchEvent(event)方法完成了不同手勢的識別。
GestureDetector這個類對外提供了兩個接口和一個外部類:

 •接口:OnGestureListener,OnDoubleTapListener
 •外部類:SimpleOnGestureListener

這個外部類,其實是兩個接口中所有函數的集成,它包含了這兩個接口里所有必須要實現的函數而且都已經重寫,但所有方法體都是空的;不同點在于:該類是static class,程序員可以在外部繼承這個類,重寫里面的手勢處理方法。 

OnGestureListener有下面的幾個動作:
 •onDown(MotionEvent e):用戶按下屏幕就會觸發。
 •onShowPress(MotionEvent e):如果按下的時間超過瞬間,而且在按下的時候沒有松開或者是拖動的,那么onShowPress就會執行,具體這個瞬間是多久,我也不清楚…
 •onLongPress(MotionEvent e):長按觸摸屏,超過一定時長,就會觸發這個事件。
觸發順序:onDown->onShowPress->onLongPress
 •onSingleTapUp(MotionEvent e):一次單獨的輕擊抬起操作,也就是輕擊一下屏幕,立刻抬起來,才會有這個觸發,當然,如果除了Down以外還有其它操作,那就不再是Single操作了,所以也就不會觸發這個事件。
觸發順序:
點擊一下非常快的(不滑動)Touchup:
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觸發。
參數解釋:
e1:第1個ACTION_DOWN MotionEvent
e2:最后一個ACTION_MOVE MotionEvent
velocityX:X軸上的移動速度,像素/秒
velocityY:Y軸上的移動速度,像素/秒
 •onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖動事件。無論是用手拖動view,或者是以拋的動作滾動,都會多次觸發,這個方法在ACTION_MOVE動作發生時就會觸發。 

OnDoubleTapListener有下面的幾個動作:
 •onSingleTapConfirmed(MotionEvent e):單擊事件。用來判定該次點擊是SingleTap而不是DoubleTap,如果連續點擊兩次就是DoubleTap手勢,如果只點擊一次,系統等待一段時間后沒有收到第二次點擊則判定該次點擊為SingleTap而不是DoubleTap,然后觸發SingleTapConfirmed事件。
觸發順序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed
 •onDoubleTap(MotionEvent e):雙擊事件。
 •onDoubleTapEvent(MotionEvent e):雙擊間隔中發生的動作。指觸發onDoubleTap以后,在雙擊之間發生的其它動作,包含down、up和move事件。 

關于onSingleTapConfirmed和onSingleTapUp的區別:onSingleTapUp,只要手抬起就會執行,而對于onSingleTapConfirmed來說,如果雙擊的話,則onSingleTapConfirmed不會執行。

使用Gesture
 •使用GestureDetector.OnGestureListener接口 

要使用OnGestureListener接口,大致有幾步要走:
1、創建OnGestureListener監聽函數:

private class gestureListener implements GestureDetector.OnGestureListener{ 

}

2、創建GestureDetector實例:
構造函數有下面三個,根據需要選擇:

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現在已經是deprecation狀態,現在推薦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()方法中,我們調用GestureDetector的onTouchEvent()方法,將捕捉到的MotionEvent交給GestureDetector來分析是否有合適的callback函數來處理用戶的手勢

public boolean onTouch(View v, MotionEvent event) {  return gestureDetectorCompat.onTouchEvent(event);  } 

 4、控件綁定

TextView tv = (TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);

 實現代碼:

<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接口
 實現OnDoubleTapListener接口的前提是先實現OnGestureListener接口,其實除了第1步,2、3、4步和上面完全一樣,不再贅述,下面看下第一步:

同時創建OnGestureListener和OnDoubleTapListener監聽函數:
方法一:新建一個類同時派生自OnGestureListener和OnDoubleTapListener:

復制代碼 代碼如下:
private class gestureListener implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{ 
    }

方法二:使用GestureDetector.setOnDoubleTapListener();函數設置監聽:

/構建GestureDetector實例  mGestureDetector = new GestureDetector(new gestureListener()); private class gestureListener implements GestureDetector.OnGestureListener{  } //設置雙擊監聽器 mGestureDetector.setOnDoubleTapListener(new doubleTapListener()); private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  } 

 注:大家可以看到無論在方法一還是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我們說過GestureDetectorCompat 的構造函數,如下:

GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener);
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler); 

 可以看到,它的兩個構造函數參數都必須是OnGestureListener的實例。所以要想使用OnDoubleTapListener的幾個函數,就必須先實現OnGestureListener。
實現代碼:

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接口,這樣需要重載接口所有的方法,適合監聽所有的手勢,如果我們只想監聽某個手勢或某幾個手勢呢,這時候就可以使用SimpleOnGestureListener類了。
它與前兩個不同的是:
1、這是一個類,在它基礎上新建類的話,要用extends派生而不是用implements繼承!
2、OnGestureListener和OnDoubleTapListener接口里的函數都是強制必須重寫的,即使用不到也要重寫出來一個空函數但在SimpleOnGestureListener類的實例或派生類中不必如此,可以根據情況,用到哪個函數就重寫哪個函數,因為SimpleOnGestureListener類本身已經實現了這兩個接口的所有函數,只是里面全是空的而已。

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的函數*****/   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的函數*****/   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.VeVB.COm/201609/yuanma/GestureDetector(VeVB.COm).rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 榆社县| 修武县| 栾城县| 柘城县| 弥勒县| 尤溪县| 沾益县| 内黄县| 阳谷县| 乐业县| 阳泉市| 扎兰屯市| 苍梧县| 镇原县| 兰溪市| 莱州市| 汝南县| 罗甸县| 图们市| 宿州市| 鄂托克旗| 佛坪县| 丰都县| 五常市| 全州县| 新和县| 宁波市| 元阳县| 乐亭县| 哈尔滨市| 芦山县| 华宁县| 武定县| 兴业县| 长兴县| 桂林市| 贵德县| 忻州市| 孟州市| 兴宁市| 保山市|