LibGDX - 在运行时激活加速度计
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LibGDX - 在运行时激活加速度计相关的知识,希望对你有一定的参考价值。
我有一个使用加速度计的应用程序,但偶尔只在极少数情况下使用。我希望通过默认禁用电池来保存电池,并且只在需要时将其打开。
我发现只有在从this site初始化应用程序时设置配置
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
androidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useCompass = false;
config.useAccelerometer = false;
MyGame myGame = new MyGame(new AndroidPlatform(this, config));
initialize(myGame , config);
}
但是在应用程序运行时我无法找到启用/禁用它的方法。有人有想法吗?
编辑:
在上面的示例中,AndroidPlatform正在核心项目中实现Platform接口。我尝试了Zoe将配置传递给平台实现并将其更改如下的想法:
@Override
public void enableAccelerometer(boolean enable) {
config.useCompass = enable;
config.useAccelerometer = enable;
}
然后在核心项目中启用加速度计时:
private void startInclineMonitoring() {
System.out.println("Before:");
System.out.println(Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer));
System.out.println(Gdx.input.isPeripheralAvailable(Input.Peripheral.Compass));
platform.enableAccelerometer(true);
System.out.println("After:");
System.out.println(Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer));
System.out.println(Gdx.input.isPeripheralAvailable(Input.Peripheral.Compass));
}
不幸的是这输出:
I/System.out: Before:
I/System.out: false
I/System.out: false
I/System.out: After:
I/System.out: false
I/System.out: false
所以,那里没有运气。
答案
看起来今天没有简单的方法可以做到这一点,但我最终做了自己的(Android)运动传感器实现。我以为我会为以后的访客分享它:
假设您拥有this wiki中所述的平台接口和平台特定实现。
首先将这些方法添加到界面中:
public interface Platform {
public void startMotionSensors(PlatformCallback<float[]> callback);
public void stopMotionSensors();
}
并在Android实现中:
public class AndroidPlatform implements Platform {
private Activity activity;
private MotionSensor motionSensor;
private Handler handler;
public AndroidPlatform(Activity activity) {
this.activity = activity;
this.motionSensor = new MotionSensor(activity);
this.handler = new Handler();
}
@Override
public void startMotionSensors(final PlatformCallback<float[]> callback) {
handler.post(new Runnable() {
@Override
public void run() {
motionSensor.start(callback);
}
});
}
@Override
public void stopMotionSensors() {
handler.post(new Runnable() {
@Override
public void run() {
motionSensor.stop();
}
});
}
}
MotionSensor类:
public class MotionSensor implements SensorEventListener {
private Activity activity;
private SensorManager sensorManager;
private float[] gravity = new float[3];
private float[] geomag = new float[3];
private float[] rotationMatrix = new float[16];
private float[] inclinationMatrix = new float[16];
private PlatformCallback<float[]> callback;
public MotionSensor(Activity activity) {
this.activity = activity;
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
gravity = event.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
geomag = event.values.clone();
break;
}
if (gravity != null && geomag != null) {
boolean success = SensorManager.getRotationMatrix(rotationMatrix,
inclinationMatrix, gravity, geomag);
if (success) {
notifyCallback(new Result(), rotationMatrix);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
private void notifyCallback(Result result, float[] rotationMatrix) {
callback.callback(result, rotationMatrix);
}
public void start(PlatformCallback<float[]> callback) {
this.callback = callback;
sensorManager = (SensorManager) activity.getSystemService(Activity.SENSOR_SERVICE);
if (sensorManager != null) {
boolean accelerometerSupport = sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
boolean magneticFieldSupport = sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_UI);
if (!accelerometerSupport || !magneticFieldSupport) {
sensorManager.unregisterListener(this);
notifyCallback(new Result(Result.STATE.FAILED, "Not supported"), null);
}
} else {
notifyCallback(new Result(Result.STATE.FAILED, "Not supported"), null);
}
}
public void stop() {
if (sensorManager != null) {
sensorManager.unregisterListener(this);
}
}
}
和PlaformCallback类:
public abstract class PlatformCallback<T> {
public void callback(final Result result, final T t) {
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
doCallback(result, t);
}
});
}
protected abstract void doCallback(Result result, T t);
}
在核心项目中,您现在可以简单地打开和关闭运动传感器:
private void startMotionSensor() {
platform.startMotionSensors(new PlatformCallback<float[]>() {
@Override
protected void doCallback(Result result, float[] rotationMatrix) {
if (result.ok()) {
// Do what you want with the rotation matrix
}
}
});
}
public void stopMotionSensor() {
platform.stopMotionSensors();
}
以上是关于LibGDX - 在运行时激活加速度计的主要内容,如果未能解决你的问题,请参考以下文章
LibGDX HelloWorld 项目在 Android 模拟器上运行时崩溃