Android中在View中如何傳遞touch事件精講

來源:互聯網
上載者:User

先來看一張圖


著作權申明:這是csdn上別人的圖,我覺得有用,就拿過來了,

然後簡單說明下:

總的來說,觸摸事件是從最外層的ViewGroup,一級一級傳遞進來的

和這相關的每個View的回調方法有三個,dispatchTouchEvent,onInterceptTouchEvent,以及大家都熟悉的ouTouchEvent

事件傳遞到一個View(ViewGroup),先回調到dispatchTouchEvent()方法,這裡如果不super的話,就直接只執行當前代碼了,super的話,到onInterceptTouchEvent()方法,這裡比較關鍵,返回false的話,說明不攔截,繼續往下傳,true的話,說明攔截,直接截斷了傳遞鏈,然後轉到本View的ouTouchEvent方法,整個就結束了。

繼續傳遞的話,如果都沒有攔截,就從子View的onTouchEvent方法一直傳上來,傳到頂層的View,假如傳遞鏈中間一個View的onTouchEvent返回true的話,

說明這個View已經處理事件了,就到此為止了,不往上傳遞了。


Android Touch事件傳遞機制解析


android系統中的每個View的子類都具有下面三個和TouchEvent處理密切相關的方法:

1)public boolean dispatchTouchEvent(MotionEvent ev)  這個方法用來分發TouchEvent
2)public boolean onInterceptTouchEvent(MotionEvent ev) 這個方法用來攔截TouchEvent
3)public boolean onTouchEvent(MotionEvent ev) 這個方法用來處理TouchEvent


測試程式介面

 下述3個Layout內含項目關聯性見如下介面圖。



 狀態1:由center處理Touch事件

Xml如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <dk.touch.MyLayout        android:id="@+id/out"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:gravity="center"            android:background="#ff345600"         >        <dk.touch.MyLayout            android:id="@+id/middle"            android:layout_width="200dp"            android:layout_height="200dp"            android:gravity="center"            android:background="#ff885678"            >            <dk.touch.MyLayout                android:id="@+id/center"                android:layout_width="50dp"                android:layout_height="50dp"                android:background="#ff345678"                android:focusable="true"                android:focusableInTouchMode="true"                android:clickable="true"                 >            </dk.touch.MyLayout>        </dk.touch.MyLayout>    </dk.touch.MyLayout></LinearLayout>

 
注意:只有center這個部分是會處理/消費 Touch事件。



事件傳遞記錄結果如上圖。

由於Down、Move、Up事件處理流程略微不同,故分開分析。


ACTION_DOWN事件處理流程:


 



首先觸摸事件發生時(ACTION_DOWN),由系統調用Activity的dispatchTouchEvent方法,分發該事件。根據觸摸事件的座標,將此事件傳遞給out的dispatchTouchEvent處理,out則調用onInterceptTouchEvent 判斷事件是由自己處理,還是繼續分發給子View。此處由於out不處理Touch事件,故根據事件發生座標,將事件傳遞給out的直接子View(即middle)。

Middle及Center中事件處理過程同上。但是由於Center組件是clickable 表示其能處理Touch事件,故center中的onInterceptTouchEvent方法將事件傳遞給center自己的onTouchEvent方法處理。至此,此Touch事件已被處理,不繼續進行傳遞。

Move和 up 事件處理流程類似,但是再center內的dispatchTouchEvent方法內被直接分配給onTouchEvent處理,不需經過onInterceptTouchEvent判斷。這是由於,android系統中將1個down事件、n個move事件、1個up事件整體作為一次邏輯上的觸控操作,Down事件已經確定了處理事件的對象,則後續的move、up事件也確定了處理事件的對象。

 
狀態2:都不處理事件

Xml如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <dk.touch.MyLayout        android:id="@+id/out"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:gravity="center"            android:background="#ff345600"         >        <dk.touch.MyLayout            android:id="@+id/middle"            android:layout_width="200dp"            android:layout_height="200dp"            android:gravity="center"            android:background="#ff885678"            >            <dk.touch.MyLayout                android:id="@+id/center"                android:layout_width="50dp"                android:layout_height="50dp"                android:background="#ff345678"                 >            </dk.touch.MyLayout>        </dk.touch.MyLayout>    </dk.touch.MyLayout></LinearLayout>


 

輕觸center部分logcat輸出結果


 

Action_down事件處理流程:

 
 
事件處理流程大致同上,區別是此狀態下,所有組件都不會處理事件,事件並不會被center的onTouchEvent方法“消費”,則事件會層層逆向傳遞迴到Activity,若Activity也不對此事件進行處理,此事件相當於消失了(無效果)。

 
對於後續的move、up事件,由於第一個down事件已經確定由Activity處理事件,故up事有由Activity的dispatchTouchEvent直接分發給自己的onTouchEvent方法處理。

 原始碼

 

package dk.touch;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;public class MainActivity extends Activity{       @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);               MyLayout out=(MyLayout) findViewById(R.id.out);        out.setName("out");               MyLayout middle=(MyLayout) findViewById(R.id.middle);        middle.setName("middle");               MyLayout center=(MyLayout) findViewById(R.id.center);        center.setName("center");    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        int action=ev.getAction();        String actionName="";        switch(action)        {        case MotionEvent.ACTION_DOWN:            actionName="ACTION_DOWN";            break;        case MotionEvent.ACTION_MOVE:            actionName="ACTION_MOVE";            break;        case MotionEvent.ACTION_UP:            actionName="ACTION_UP";            break;        }        System.out.println("Activity"+"|"+actionName+":dispatchTouchEvent");        return super.dispatchTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        int action=event.getAction();        String actionName="";        switch(action)        {        case MotionEvent.ACTION_DOWN:            actionName="ACTION_DOWN";            break;        case MotionEvent.ACTION_MOVE:            actionName="ACTION_MOVE";            break;        case MotionEvent.ACTION_UP:            actionName="ACTION_UP";            break;        }               System.out.println("Activity"+"|"+actionName+":onTouchEvent");        return super.onTouchEvent(event);    }      }package dk.touch;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.LinearLayout;public class MyLayout extends LinearLayout {    private String name="";    public MyLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        int action=event.getAction();        String actionName="";        switch(action)        {        case MotionEvent.ACTION_DOWN:            actionName="ACTION_DOWN";            break;        case MotionEvent.ACTION_MOVE:            actionName="ACTION_MOVE";            break;        case MotionEvent.ACTION_UP:            actionName="ACTION_UP";            break;        }        System.out.println(name+"|"+actionName+":onTouchEvent");        return super.onTouchEvent(event);    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        int action=ev.getAction();        String actionName="";        switch(action)        {        case MotionEvent.ACTION_DOWN:            actionName="ACTION_DOWN";            break;        case MotionEvent.ACTION_MOVE:            actionName="ACTION_MOVE";            break;        case MotionEvent.ACTION_UP:            actionName="ACTION_UP";            break;        }        System.out.println(name+"|"+actionName+":dispatchTouchEvent");        return super.dispatchTouchEvent(ev);    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        int action=ev.getAction();        String actionName="";        switch(action)        {        case MotionEvent.ACTION_DOWN:            actionName="ACTION_DOWN";            break;        case MotionEvent.ACTION_MOVE:            actionName="ACTION_MOVE";            break;        case MotionEvent.ACTION_UP:            actionName="ACTION_UP";            break;        }        System.out.println(name+"|"+actionName+":onInterceptTouchEvent");        return super.onInterceptTouchEvent(ev);    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }   }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.