簡體   English   中英

LibGDX-在運行時激活加速度計

[英]LibGDX - Activate accelerometer in runtime

我有一個使用加速度計的應用程序,但僅在極少數情況下偶爾使用。 我想保留電池,方法是默認將其禁用,並僅在需要時才將其打開。

我唯一發現的是從此站點初始化應用程序時設置配置

@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)運動傳感器實現。 我想與大家分享一下:

這假定您具有本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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM