我正在尝试使用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) {
}
};
什么是获取手机角度仅在手机倾斜后的最佳方法? 对于糟糕的英语,我感到抱歉。 谢谢。