Android 如何计算设备旋转的角度?
我正在开发一个应用程序,需要检测设备旋转的角度。我尝试过使用OrientationEventListener之类的方法。这很好,但仅适用于设备旋转后位于同一平面的情况。我对检测旋转角度感兴趣,在旋转角度中,设备的平面也会发生变化。为了清晰起见,请参见下图Android 如何计算设备旋转的角度?,android,android-sensors,Android,Android Sensors,我正在开发一个应用程序,需要检测设备旋转的角度。我尝试过使用OrientationEventListener之类的方法。这很好,但仅适用于设备旋转后位于同一平面的情况。我对检测旋转角度感兴趣,在旋转角度中,设备的平面也会发生变化。为了清晰起见,请参见下图 下面是一个示例代码 取自现场: 你对Hi gilonm感兴趣我认为这并不能解决问题。此外,现在不推荐使用类型_定向。 package gyroexample.com.example; import android.app.Activity;
下面是一个示例代码 取自现场:
你对Hi gilonm感兴趣我认为这并不能解决问题。此外,现在不推荐使用类型_定向。
package gyroexample.com.example;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class AccessGyroscope extends Activity implements SensorEventListener
{
//a TextView
private TextView tv;
//the Sensor Manager
private SensorManager sManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the TextView from the layout file
tv = (TextView) findViewById(R.id.tv);
//get a hook to the sensor service
sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
//when this Activity starts
@Override
protected void onResume()
{
super.onResume();
/*register the sensor listener to listen to the gyroscope sensor, use the
callbacks defined in this class, and gather the sensor information as quick
as possible*/
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
}
//When this Activity isn't visible anymore
@Override
protected void onStop()
{
//unregister the sensor listener
sManager.unregisterListener(this);
super.onStop();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing.
}
@Override
public void onSensorChanged(SensorEvent event)
{
//if sensor is unreliable, return void
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
return;
}
//else it will output the Roll, Pitch and Yawn values
tv.setText("Orientation X (Roll) :"+ Float.toString(event.values[2]) +"\n"+
"Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
"Orientation Z (Yaw) :"+ Float.toString(event.values[0]));
}
}