如何在 Android 中使用陀螺仪
How to use gyroscope in Android
我需要在我的应用程序中计算 yz 角度。我正在尝试为此使用陀螺仪,但它不起作用。我的 phone 甚至没有进入函数 onSensorChanged。你能帮助我吗?这是我的代码
主要活动:
public class mainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
private float xy_angle;
private float xz_angle;
private float yz_angle;
private TextView yzPlane;
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
protected void onResume()
{
super.onResume();
}
protected void onPause()
{
super.onPause();
}
public void onSensorChanged(SensorEvent sensorEvent)
{
xy_angle = sensorEvent.values[0];
xz_angle = sensorEvent.values[1];
yz_angle = sensorEvent.values[2];
Log.i("Test", "Trying to change sensor" + String.valueOf(xy_angle));
yzPlane.setText("Angle is: " + String.valueOf(yz_angle));
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
yzPlane = (TextView) findViewById(R.id.yzPlane);
}
我在清单文件中也有使用功能:
<uses-feature android:name="android.hardware.sensor.gyroscope" android:required="true"/>
我有索尼 Xperia M2,所以它有陀螺仪。但是亚行给我这个错误:
21681-21681/com.example.orientationsensor E/SensorManager﹕ sensor or listener is null
a̶s̶i̶k̶k̶n̶o̶w̶,̶x̶p̶e̶e̶i̶i̶a̶m̶m̶2̶2̶o̶e̶e̶e̶e̶s̶n̶t̶加速度计并查看是否调用了 onSensorChanged
。可能是错误...
我需要在我的应用程序中计算 yz 角度。我正在尝试为此使用陀螺仪,但它不起作用。我的 phone 甚至没有进入函数 onSensorChanged。你能帮助我吗?这是我的代码
主要活动:
public class mainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
private float xy_angle;
private float xz_angle;
private float yz_angle;
private TextView yzPlane;
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
protected void onResume()
{
super.onResume();
}
protected void onPause()
{
super.onPause();
}
public void onSensorChanged(SensorEvent sensorEvent)
{
xy_angle = sensorEvent.values[0];
xz_angle = sensorEvent.values[1];
yz_angle = sensorEvent.values[2];
Log.i("Test", "Trying to change sensor" + String.valueOf(xy_angle));
yzPlane.setText("Angle is: " + String.valueOf(yz_angle));
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
yzPlane = (TextView) findViewById(R.id.yzPlane);
}
我在清单文件中也有使用功能:
<uses-feature android:name="android.hardware.sensor.gyroscope" android:required="true"/>
我有索尼 Xperia M2,所以它有陀螺仪。但是亚行给我这个错误:
21681-21681/com.example.orientationsensor E/SensorManager﹕ sensor or listener is null
a̶s̶i̶k̶k̶n̶o̶w̶,̶x̶p̶e̶e̶i̶i̶a̶m̶m̶2̶2̶o̶e̶e̶e̶e̶s̶n̶t̶加速度计并查看是否调用了 onSensorChanged
。可能是错误...