編輯:關於android開發
我們今天要學習的就是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>
Android手機的通訊錄聯系人全部都存在系統的數據庫中,如果須要獲得通訊裡聯系人的信息就須要訪問系統的數據庫,才能將信息拿出來。 這一篇文章我主要
Android新手入門2016(10)--GridView GridView跟ListView一樣是多控件布局。實現九宮圖是最方便的。 還是先看看圖,沒圖說個雞雞是不是
Android 5.0 Settings源碼簡要分析 概述: 先聲明:本人工作快兩年了,仍是菜鳥級別的,慚愧啊!以前遇到好多知識點都沒有記錄下來,感覺挺可惜的,現在有機會
Activity界面切換動畫特效。,activity特效效果圖: 結構圖: 測試代碼: 布局: 1 <?xml version=1