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

Android 觸控螢幕單點測試:拆解 MotionEvent 的四種事件

編輯:關於 Android 開發

做 Android 遊戲開發,必須對手機上的各項硬體了如指掌,而其中最重要的莫過於觸控螢幕。一切的起點,是搞清楚螢幕單點觸發了哪些事件。

先直接看程式碼,再回頭解釋原理:

package eoe.gamedev;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class SingleTouchTest extends Activity implements OnTouchListener {
    StringBuilder builder = new StringBuilder();
    TextView textView;

    public void onCreate(Bundle state) {
        super.onCreate(state);
        textView = new TextView(this);
        textView.setText("Touch and Drag one finger");
        textView.setOnTouchListener(this);
        setContentView(textView);
    }

    public boolean onTouch(View arg0, MotionEvent arg1) {
        builder.setLength(0);
        switch (arg1.getAction()) {
            case MotionEvent.ACTION_DOWN:
                builder.append("down, ");
                break;
            case MotionEvent.ACTION_MOVE:
                builder.append("move, ");
                break;
            case MotionEvent.ACTION_UP:
                builder.append("up ,");
                break;
            case MotionEvent.ACTION_CANCEL:
                builder.append("cancel, ");
                break;
        }
        builder.append(arg1.getX());
        builder.append(", ");
        builder.append(arg1.getY());
        Log.d("TouchTest", builder.toString());
        textView.setText(builder.toString());
        return true;
    }
}

四個關鍵 Action

要攔截觸控事件,需實作 OnTouchListener 介面並覆寫 onTouch(View, MotionEvent) 方法。而 MotionEvent 提供四種 Action,分別是:

  • ACTION_DOWN:手指按下螢幕的那一刻。
  • ACTION_MOVE:手指在螢幕上移動。
  • ACTION_UP:手指離開螢幕。
  • ACTION_CANCEL:手勢被系統取消,例如上層元件中途攔截了事件。

搭配 getX() 與 getY() 就能取得觸發當下的座標。上面這個範例做的事很單純:把事件類型與座標拼成字串,同時印到 logcat 並顯示在畫面上。跑起來之後用手指在螢幕上滑動,你就能親眼看到 down、move、up 的序列,以及每個 move 事件帶來的座標變化。

這個測試看起來樸素,卻是理解觸控的關鍵一課。許多人直覺以為「點一下」就是一個單一事件,實際上它是一整串:一次 ACTION_DOWN、若干次 ACTION_MOVE(即使手指幾乎沒動也可能有)、最後一次 ACTION_UP。若你要實作「點擊」而非「觸碰」的判斷,就必須自己比對 down 與 up 之間的時間差與位移量——這正是 GestureDetector 存在的理由。

另外提醒,範例中的 onTouch 回傳 true,表示事件已被消費、不再往下傳遞。若回傳 false,事件會繼續傳給其他監聽者或改用其他處理路徑,這個返回值在多層 View 巢狀結構中會直接影響事件分發的走向,值得特別留意。

清單宣告

最後附上本例的 AndroidManifest.xml,其中同時宣告了後續章節會用到的多點觸控、按鍵、加速度計等測試 Activity,並補上網路與外部儲存等權限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="eoe.gamedev"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity
            android:name=".AndroidBasis"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SingleTouchTest" android:label="SingleTouchTest"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation"></activity>
        <activity android:name=".MultiTouchTest" android:label="MultiTouchTest"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation"></activity>
        <!-- 其餘測試 Activity 依此類推 -->
    </application>

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
</manifest>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved