相关疑难解决方法(0)

如何获得带有传感器的手机的角度/度数?

我正在尝试使用TYPE_ACCELEROMETER传感器获取电话角度。我的目标是仅在手机倾斜后才能获得角度值。可以,但是问题是当我将手机正面朝上放在桌子上时,仍然说isLandscape = true;

private boolean isLandscape;

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(
                                          Sensor.TYPE_ACCELEROMETER),1000000);
private final SensorEventListener mSensorListener = new SensorEventListener() { 
    @Override
    public void onSensorChanged(SensorEvent mSensorEvent) {  
        float X_Axis = mSensorEvent.values[0];
        float Y_Axis = mSensorEvent.values[1];
        double angle = Math.atan2(X_Axis, Y_Axis)/(Math.PI/180);

        if(!isLandscape) {                  
            if(angle > 80) {
                Orientation = 90;
                isLandscape = true;
            }
        }
        else 
        {

            if(Math.abs(angle) < 10) {
                Orientation = 0;  //portrait
                isLandscape = false;
            }
        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
};
Run Code Online (Sandbox Code Playgroud)

仅在手机倾斜后才能获得手机角度的最佳方法是什么?对不起,英语不好,

谢谢。

android android-sensors android-orientation

5
推荐指数
1
解决办法
6265
查看次数