新建一个Android项目,File -> New -> Project -> Android Project。我选的SDK是1.6的,最低选择应该是1.5.
新建一个文件夹libs(这个名字是固定的,不能随意改),将下载到的jar复制进去,android平台只需要复制gdx.jar和gdx-backend-android.jar。
增加引用,然后将armeabi和armeabi-v7a两个文件夹复制到libs文件夹中。
然后写个小东西测试一下环境有没有问题。风之境地
新建类HelloGameActivity,继承AndroidApplication类。
package com.cnblogs.htynkn;
import com.badlogic.gdx.backends.android.AndroidApplication;
import android.os.Bundle;
public class HelloGameActivity extends AndroidApplication {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initialize(new FirstGame(), false);
}
}
initialize(new FirstGame(), false);
就是启动游戏的关键,FirstGame是一个实现了ApplicationListener的类。
代码如下
package com.cnblogs.htynkn;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class FirstGame implements ApplicationListener {
//绘图用的SpriteBatch
private SpriteBatch batch;
@Override
public void create() {
batch = new SpriteBatch(); //实例化
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //清屏
batch.begin();
batch.end();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}
环境搭建就说到这里,下一篇是关于图片绘制的。
注意:
如果仔细观察Logcat,就会发现一个error:
E/libEGL(382): couldn't load <libhgl.so> library (Cannot load library: load_library[984]: Library 'libhgl.so' not found)
这个没什么关系,这是系统尝试加载硬件OpenGL驱动失败了,然后回到软件的处理方法上,不是缺陷或者错误。
adb pull /sdcard/mobileoa/log/2012-01-09.log c:\
通过handler结合Html刷新dialog界面的显示内容:
package com.pro.activity;
import java.util.List;
import com.pro.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
public class SensorCheckActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener,SensorEventListener{
private SharedPreferences sp;
private SensorManager sManager;
private Sensor sensor;
private Spanned msgMes;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.sensor);
sp = PreferenceManager.getDefaultSharedPreferences(this);
sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// sensor = sManager.getDefaultSensor(Sensor.TYPE_ALL);
}
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case Sensor.TYPE_ACCELEROMETER:
float[] data = (float[]) msg.obj;
// msgMes = "X轴方向:"+data[0]+"\nY周方向:"+data[1]+"\nZ轴方向:"+data[2];
msgMes = Html.fromHtml("<br> X轴方向加速度:"+data[0]+"<br> Y周方向加速度:"+data[1]+"<br> Z轴方向加速度:"+data[2]+"<br>");
textView.setText(msgMes);
break;
case Sensor.TYPE_LIGHT:
float[] light = (float[]) msg.obj;
msgMes = Html.fromHtml("<br> 当前光线强度:"+light[0]+"<br>");
textView.setText(msgMes);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
float[] magnetic = (float[]) msg.obj;
msgMes = Html.fromHtml("<br> X轴方向磁感应:"+magnetic[0]+"<br> Y周方向磁感应:"+magnetic[1]+"<br> Z轴方向磁感应:"+magnetic[2]+"<br>");
textView.setText(msgMes);
break;
case Sensor.TYPE_PROXIMITY:
float[] proximity = (float[]) msg.obj;
msgMes = Html.fromHtml("<br> 当前近距感应值:"+proximity[0]+"<br>");
textView.setText(msgMes);
break;
case Sensor.TYPE_ORIENTATION:
float[] orientation = (float[]) msg.obj;
msgMes = Html.fromHtml("<br> X轴方向:"+orientation[0]+"<br> Y周方向:"+orientation[1]+"<br> Z轴方向:"+orientation[2]+"<br>");
textView.setText(msgMes);
break;
case Sensor.TYPE_PRESSURE:
float[] pressure = (float[]) msg.obj;
msgMes = Html.fromHtml("<br> 当前压力感应值:"+pressure[0]+"<br>");
textView.setText(msgMes);
break;
case Sensor.TYPE_TEMPERATURE:
float[] temperaure = (float[]) msg.obj;
msgMes = Html.fromHtml("<br> 当前温度感应值:"+temperaure[0]+"<br>");
textView.setText(msgMes);
break;
default:
break;
}
};
};
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
sp.registerOnSharedPreferenceChangeListener(this);
// sManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
sp.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// sManager.unregisterListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
if("key_acceleration".equals(preference.getKey())){//加速度
showDialogView(this, Sensor.TYPE_ACCELEROMETER, (String) preference.getTitle());
}else if("key_ray".equals(preference.getKey())){//光线
showDialogView(this, Sensor.TYPE_LIGHT, (String) preference.getTitle());
}else if("key_dis".equals(preference.getKey())){//近距
showDialogView(this, Sensor.TYPE_PROXIMITY, (String) preference.getTitle());
}else if("key_temperature".equals(preference.getKey())){//温度
showDialogView(this, Sensor.TYPE_TEMPERATURE, (String) preference.getTitle());
}else if("key_way".equals(preference.getKey())){//方向
showDialogView(this, Sensor.TYPE_ORIENTATION, (String) preference.getTitle());
}else if ("key_magnetic".equals(preference.getKey())) {//磁场
showDialogView(this, Sensor.TYPE_MAGNETIC_FIELD, (String) preference.getTitle());
}else if("key_pressure".equals(preference.getKey())){//压力
showDialogView(this, Sensor.TYPE_PRESSURE, (String) preference.getTitle());
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
int type = event.sensor.getType();//.accuracy;
Message message = new Message();
switch (type) {
case Sensor.TYPE_ACCELEROMETER://SensorManager.SENSOR_ACCELEROMETER://加速度
float[] data = new float[3];
data[0] = event.values[0]; //x
data[1] = event.values[1]; //y
data[2] = event.values[2]; //z轴上的加速度
message.what = type;
message.obj = data;
handler.sendMessage(message);
break;
case Sensor.TYPE_ORIENTATION://SensorManager.SENSOR_ORIENTATION://方向
float[] orientation = new float[3];
orientation[0] = event.values[0]; //x
orientation[1] = event.values[1]; //y
orientation[2] = event.values[2]; //z轴上的方向
message.what = type;
message.obj = orientation;
handler.sendMessage(message);
break;
case Sensor.TYPE_PROXIMITY://SensorManager.SENSOR_PROXIMITY://距离
float[] proximity = new float[1];
proximity[0] = event.values[0]; //
message.what = type;
message.obj = proximity;
handler.sendMessage(message);
break;
case Sensor.TYPE_PRESSURE://Sensor.TYPE_PRESSURE://压力
float[] pressure = new float[1];
pressure[0] = event.values[0]; //
message.what = type;
message.obj = pressure;
handler.sendMessage(message);
break;
case Sensor.TYPE_LIGHT://SensorManager.SENSOR_LIGHT://光线
float[] light = new float[1];
light[0] = event.values[0]; //
message.what = type;
message.obj = light;
handler.sendMessage(message);
break;
case Sensor.TYPE_TEMPERATURE://SensorManager.SENSOR_TEMPERATURE://温度
float[] temperature = new float[1];
temperature[0] = event.values[0]; //
message.what = type;
message.obj = temperature;
handler.sendMessage(message);
break;
case Sensor.TYPE_MAGNETIC_FIELD://磁场
float[] magnetic = new float[3];
magnetic[0] = event.values[0]; //x
magnetic[1] = event.values[1]; //y
magnetic[2] = event.values[2]; //z轴上的磁感应
message.what = type;
message.obj = magnetic;
handler.sendMessage(message);
break;
default:
break;
}
}
/**
* 显示不同感应器的测试视图
*/
private void showDialogView(Context ctx,int sensorType,String msgTitle){
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
if(sManager != null){
// sensor = sManager.getDefaultSensor(Sensor.TYPE_ALL);
sensor = sManager.getDefaultSensor(sensorType);
if(sensor != null){
sManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}else{//手机不支持此类感应器类型
msgMes = Html.fromHtml("<br> 该手机不支持此类感应器类型");
builder.setMessage(msgMes);
}
}
textView = new TextView(ctx);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(lp);
builder.setTitle(msgTitle);
builder.setIcon(R.drawable.icon);
// builder.setMessage(msgMes);
builder.setView(textView);
builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sManager.unregisterListener(SensorCheckActivity.this);
}
});
builder.show()
.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
textView.setText(msgMes);
}
});
}
}