手勢操作原理
首先,在android系統中,每一次手勢互動都會依照以下順序執行。
1. 接觸接觸屏一刹那,觸發一個motionevent事件。
2. 該事件被ontouchlistener監聽,在其ontouch()方法裡獲得該motionevent對象。
3. 通過gesturedetector(手勢識别器)轉發次motionevent對象至ongesturelistener。
4. ongesturelistener獲得該對象,聽根據該對象封裝的的資訊,做出合适的回報。
這個順序可以說就是手勢操作的原理。
手勢操作類和接口
下面一同來了解一下motionevent、gesturedetector和ongesturelistener。
motionevent: 這個類用于封裝手勢、觸摸筆、軌迹球等等的動作事件。其内部封裝了兩個重要的屬性x和y,這兩個屬性分别用于記錄橫軸和縱軸的坐标。
gesturedetector: 識别各種手勢。
ongesturelistener: 這是一個手勢互動的監聽接口,其中提供了多個抽象方法,并根據gesturedetector的手勢識别結果調用相對應的方法。
手勢操作執行個體
下面我再通過一個切換美女圖檔的代碼示例,示範一下手勢互動的實作,讓大夥對上面的執行順序,以及各手勢動作的區分有一個更加深刻的了解和記憶。
xml/html代碼
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://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>
java代碼
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--;
//重寫ontouchlistener的ontouch方法
//此方法在觸摸屏被觸摸,即發生觸摸事件(接觸和撫摸兩個事件,挺形象)的時候被調用。
public boolean ontouch(view v, motionevent event) {
detector.ontouchevent(event);
return true;
//在按下動作時被調用
public boolean ondown(motionevent e) {
return false;
//在抛擲動作時被調用
public boolean onfling(motionevent e1, motionevent e2, float velocityx,
float velocityy) {
//velocityx表示橫向的移動,根據手指移動的方向切換女孩
if(velocityx < 0){
gonext();
}else if(velocityx > 0){
goprevious();
}
//在長按時被調用
public void onlongpress(motionevent e) {
//在滾動時調用
public boolean onscroll(motionevent e1, motionevent e2, float distancex,
float distancey) {
//在按住時被調用
public void onshowpress(motionevent e) {
//在擡起時被調用
public boolean onsingletapup(motionevent e) {
}
手勢操作各個方法的含義
在剛開始學android的時候,就覺得google的文檔不咋樣,在研究手勢時,更加的感覺google的文檔寫得實在是太差了。很多常量, 屬性和方法,居然連個描述都沒有。沒有描述也就罷了,但是ongesturelistener裡手勢這麼多,它也沒有一個介紹說明,在沒有進行不斷才嘗試 之前,誰能搞懂onlongpress和onshowpress,onscroll和onfling的關系與差别嗎?google真的需要在文檔方面做一次大手術了。不過好在經過鄙人不斷反複的嘗試。從個人的角度為這幾個手勢動作做出了定義。
按下(ondown): 剛剛手指接觸到觸摸屏的那一刹那,就是觸的那一下。
抛擲(onfling): 手指在觸摸屏上迅速移動,并松開的動作。
長按(onlongpress): 手指按在持續一段時間,并且沒有松開。
滾動(onscroll): 手指在觸摸屏上滑動。
按住(onshowpress): 手指按在觸摸屏上,它的時間範圍在按下起效,在長按之前。
擡起(onsingletapup):手指離開觸摸屏的那一刹那。
除了這些定義之外,鄙人也總結了一點算是經驗的經驗吧,在這裡和大家分享一下。
任何手勢動作都會先執行一次按下(ondown)動作。
長按(onlongpress)動作前一定會執行一次按住(onshowpress)動作。
按住(onshowpress)動作和按下(ondown)動作之後都會執行一次擡起(onsingletapup)動作。
長按(onlongpress)、滾動(onscroll)和抛擲(onfling)動作之後都不會執行擡起(onsingletapup)動作。
from:http://www.jizhuomi.com/android/course/264.html
歡迎加群互相學習,共同進步。qq群:ios: 58099570 | android: 330987132 | go:217696290 | python:336880185 | 做人要厚道,轉載請注明出處!http://www.cnblogs.com/sunshine-anycall/p/4845271.html