java - LibGDX - 在运行时激活加速度计

标签 java android 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 接口(interface)。我尝试了 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) 运动传感器。我想我会把它分享给 future 的访客:

这假设您有自己的平台接口(interface)和特定于平台的实现,如 this wiki 中所述。 .

首先将这些方法添加到接口(interface)中:

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();
}

关于java - LibGDX - 在运行时激活加速度计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48853533/

相关文章:

Java:如何在 Android Studio 中生成随机信号?

java - 如何让我的主角朝某个物体移动? LibGDX/Java

gradle - JsInterop "com is not defined"

android - 使用自定义适配器/layoutinflator 时垂直对齐 textview 列

android - 在android上录制视频的代码

java - 在 Libgdx 中嵌入浏览器

java - 有效期控制程序

Java:三角函数和双重误差导致 NaN

java - 如何合并一个音频和视频文件 -- Xuggler

java - 使用 JFileChooser 设置默认保存扩展