@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
int orientation = Configuration.ORIENTATION_UNDEFINED;
float X = -values[0];
float Y = -values[1];
float Z = -values[2];
float magnitude = X * X + Y * Y;
// Don't trust the angle if the magnitude is small compared to the y
// value
// 这里是关键,来至于谷歌官方
if (magnitude * 4 >= Z * Z) {
float OneEightyOverPi = 57.29577957855f;
float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi;
orientation = 90 - (int) Math.round(angle);
// normalize to 0 - 359 range
while (orientation >= 360) {
orientation -= 360;
}
while (orientation < 0) {
orientation += 360;
}
}
if(orientation < 45 || orientation >= 315){
screentDirection = 0;
}else if(orientation >= 45 && orientation < 135){
screentDirection = 90;
}else if(orientation >= 135 && orientation < 225){
screentDirection = 180;
}else{
screentDirection = 270;
}
} @Override
public void onSensorChanged(SensorEvent event) { float[] values = event.values; int orientation = Configuration.ORIENTATION_UNDEFINED; float X = -values[0]; float Y = -values[1]; float Z = -values[2]; float magnitude = X * X + Y * Y; // Don't trust the angle if the magnitude is small compared to the y // value // 这里是关键,来至于谷歌官方 if (magnitude * 4 >= Z * Z) { float OneEightyOverPi = 57.29577957855f; float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi; orientation = 90 - (int) Math.round(angle); // normalize to 0 - 359 range while (orientation >= 360) { orientation -= 360; } while (orientation < 0) { orientation += 360; } } if(orientation < 45 || orientation >= 315){ screentDirection = 0; }else if(orientation >= 45 && orientation < 135){ screentDirection = 90; }else if(orientation >= 135 && orientation < 225){ screentDirection = 180; }else{ screentDirection = 270; } }