Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 讓人又愛又恨的觸摸機制(二):以程式碼驗證事件分發

編輯:關於 Android 程式設計

概述:上一篇已從原理層面講解了 Android 觸摸機制的運作模式,這次我們透過實際程式碼來驗證這個結論——是驢子是馬,拉出來遛一遛便知。另外筆者學識有限,若有任何問題,歡迎交流指正。

先補充一個重要觀念:上一篇曾提到三個與觸摸機制息息相關的方法(onTouchEvent、onInterceptTouchEvent、dispatchTouchEvent),但更精確地說,只有 ViewGroup 才完整擁有這三個方法;而 View 並沒有 onInterceptTouchEvent。原因很簡單——View 已是最小單位,底下沒有其他子元件可以攔截了。

案例驅動

先拋出一個問題供思考:Activity 和 View 究竟是什麼關係?兩者看似都是介面,但背後另有玄机。以下透過幾個自訂元件觀察事件的分發流程。

MainActivity

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                System.out.println("MainActivity---dispatchTouchEvent---ACTION_DOWN");
                break;
            case MotionEvent.ACTION_UP:
                System.out.println("MainActivity---dispatchTouchEvent---ACTION_UP");
                break;
        }
        boolean result = super.dispatchTouchEvent(event);
        System.out.println("MainActivity---dispatchTouchEvent---result:" + result);
        return result;
    }
}

自訂 Button(最小 View 單位)

作為最小 View 單位,Button 沒有 onInterceptTouchEvent 方法可以覆寫,只能實作另外兩個方法:

public class MyButton extends Button {
    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) System.out.println("MyButton---onTouchEvent---ACTION_DOWN");
        if (action == MotionEvent.ACTION_MOVE) System.out.println("MyButton---onTouchEvent---ACTION_MOVE");
        if (action == MotionEvent.ACTION_UP) System.out.println("MyButton---onTouchEvent---ACTION_UP");
        return true;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        boolean result = super.dispatchTouchEvent(event);
        System.out.println("MyButton---dispatchTouchEvent---result:" + result);
        return result;
    }
}

自訂 ViewGroup 子類 MyLinearLayout

作為 LinearLayout 的子類,它擁有 onInterceptTouchEvent 方法,可以選擇是否攔截子 View 的事件。以下範例中刻意將 onInterceptTouchEvent 的結果設為 true,模擬攔截行為:

public class MyLinearLayout extends LinearLayout {
    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean result = super.onTouchEvent(event);
        System.out.println("MyLinearLayout---onTouchEvent---result:" + result);
        return result;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean result = super.onInterceptTouchEvent(event);
        result = true; // 強制攔截
        System.out.println("MyLinearLayout---onInterceptTouchEvent---result:" + result);
        return result;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        boolean result = super.dispatchTouchEvent(event);
        System.out.println("MyLinearLayout---dispatchTouchEvent---result:" + result);
        return result;
    }
}

自訂 ImageView

和自訂 Button 相同,ImageView 作為最小 View 單位,同樣不存在 onInterceptTouchEvent 方法,只能實作 dispatchTouchEvent 與 onTouchEvent。

布局檔

最後來看布局,結構相當簡單,將上述自訂元件依序排列即可:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:textStyle="bold"
        android:text="@string/touch" />

    <com.example.touchevent.MyButton
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginTop="10dp"
        android:text="@string/mybutton1" />

    <com.example.touchevent.MyLinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/button1"
        android:layout_marginTop="50dp"
        android:gravity="center" >
        <com.example.touchevent.MyImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitCenter"
            android:src="@drawable/jacky" />
    </com.example.touchevent.MyLinearLayout>
</RelativeLayout>

透過觀察 LogCat 的輸出順序,你便能清楚掌握事件從 Activity 到 ViewGroup、再到子 View 的分發與回傳路徑,真正理解 Android 觸摸機制的運作精髓。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved