相关疑难解决方法(0)

Android:从Azimuth,roll&pitch获取设备方向

我需要获得更新的设备方向,但我必须将我的活动修复为Portrait(我在布局xml文件中做了),这阻止我使用它:

int rotation = getWindowManager().getDefaultDisplay().getRotation();
Run Code Online (Sandbox Code Playgroud)

因为它总是给我画像旋转,

所以,我试图依靠传感器.我发现它Sensor.TYPE_ORIENTATION已被弃用,所以我使用Sensor.TYPE_ACCELEROMETER&的组合,Sensor.TYPE_MAGNETIC_FIELD这里是事件监听器:

SensorEventListener sensorEventListener = new SensorEventListener() {
    float[] mGravity;
    float[] mGeomagnetic;

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
            mGravity = event.values;
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
            mGeomagnetic = event.values;
        if (mGravity != null && mGeomagnetic != null) {
            float R[] = new float[9];
            float I[] = new float[9];
            boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
            if (success) {
                float orientationData[] = new float[3];
                SensorManager.getOrientation(R, orientationData);
                azimuth …
Run Code Online (Sandbox Code Playgroud)

android rotation android-sensors

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

android-sensors ×1

rotation ×1