編輯: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:觸控手勢被取消。
<?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() 座標,我們就能精準掌握使用者手指的每一次按壓與移動,這是打造直覺觸控互動、乃至遊戲操作手感的第一塊基石。
Android BroadcastReceiver 使用解析
BroadcastReceiver 是 Android 四大元件之一,用於接收系統或應用送出的廣播 Intent。它常用於監聽網路變化、電量、時區、語言切換等事件。 基本
Android 廣播、服務、數據庫、通知、包等核心術語圖解
簡介Android 中運用了大量機制,例如廣播、服務、數據庫、通知、包等等。本文用圖文並茂的方式,對這些核心概念做一個直觀的梳理。什麼是廣播?Broadcast 是一種在
AutoCompleteTextView 自動完成實作:單行與多行建議輸入
當使用者在可編輯文字欄位中打字時,若 App 能即時給出建議清單,輸入效率與正確率都會明顯提升。Android 為此提供了 AutoCompleteTextView:建議
LinearLayout 與 RelativeLayout 搭配實現精準版面:padding、margin、gravity 一次搞懂
四個最容易混淆的概念 padding 和 margin 都叫「邊距」,關鍵差別在於「相對誰」: · padding:控件內容相對控件自身邊緣的距離。· margin:控件