|
我们今天要学习的就是android游戏开发必须对手机的各个设备非常了解。其中最重要的设备之一就是触摸屏。所以我们这里重点研究触摸屏的单击。
我们直接看代码吧。这里是触摸屏单击的测试代码:
java代码: 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有4个Action,分别是 MotionEvent.ACTION_DOWN, ACTION_UP, ACTION_MOVE, ACTION_CANCEL。
意思分别是触摸屏按下,松开,移动,和取消。
java代码: <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=http://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=".LifeCycleTest" android:label="LifeCycleTest" android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</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
android:name=".KeyTest"
android:label="KeyTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".AccelerometerTest"
android:label="AccelerometerTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".AssetsTest"
android:label="AssetsTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".ExternalStorageTest"
android:label="ExternalStorageTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".SoundPoolTest"
android:label="SoundPoolTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".MediaPlayerTest"
android:label="MediaPlayerTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".RenderViewTest"
android:label="RenderViewTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".BitmapTest"
android:label="BitmapTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".FontTest"
android:label="FontTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</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> |
|