java - Android 传感器直立旋转

标签 java android android-sensors

我知道有关于此的主题,并且我已经阅读它们好几天了。

我认为我的问题更具体。我正在尝试让设备绕 y 轴旋转。

如果我没猜错的话,它被称为方位角

当我运行应用程序时,在调试日志记录 value[0]、values[1] 和 value[2] 后,它在 logcat 中返回 0.0

代码:

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        SensorEventListener SEL = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {

                mSensorManager.getRotationMatrix(rotationMatrix, I, gravity, geomagnetic);

                mSensorManager.getOrientation(rotationMatrix, values);

                accy = values[0];

                System.out.println(accy);
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        };

        int rate = SensorManager.SENSOR_DELAY_GAME;
        mSensorManager.registerListener(SEL, accelerometer, rate);
        mSensorManager.registerListener(SEL, magnetometer, rate);

日志猫:

09-05 17:34:02.622    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.642    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.642    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.662    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.662    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.682    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.682    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.702    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.702    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.722    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.722    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.742    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.742    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.762    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.762    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.782    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.782    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.802    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.802    6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0

最佳答案

从上面的代码中,mSensorManager.getRotationMatrix(rotationMatrix, I,重力,地磁);中的参数gravitygeotropic可能是zero array 因此你的 accy 总是 zero

关于java - Android 传感器直立旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25694307/

相关文章:

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link failure

java - 使用 Java 的 Kafka 流 : Send TO multiple topics

android - 无法验证我的 ACS token

java - android - 获取软键盘按键

android - 在android中检测抖动

android - 更新到 2.3.5 后 GYROSCOPE 不工作

java - 文件/目录同步算法

java - 检查单选按钮是否选择了特定 Activity

java - Android 资源链接在 Apk 构建上失败

java - Android:onSensorChanged 的​​替代方案?当手机背面放在 table 上时(没有移动时)没有加速度计数据