Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 觸控螢幕單擊事件測試

編輯: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;
    }
}

原理說明

單擊螢幕需要實作 OnTouchListener,並實作 public boolean onTouch(View arg0, MotionEvent arg1) 方法。MotionEvent 有四個 Action,分別是:

  • MotionEvent.ACTION_DOWN:觸控螢幕按下。
  • MotionEvent.ACTION_UP:手指離開、放開。
  • MotionEvent.ACTION_MOVE:手指移動。
  • MotionEvent.ACTION_CANCEL:觸控手勢被取消。

AndroidManifest.xml 配置

<?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=".SingleTouchTest"
            android:label="SingleTouchTest"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </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>

透過監聽這四種觸控狀態並讀取 getX()、getY() 座標,我們就能精準掌握使用者手指的每一次按壓與移動,這是打造直覺觸控互動、乃至遊戲操作手感的第一塊基石。

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