我试图在Android工作室中获得陀螺仪传感器结果。然后显示指示方向的箭头UI。我写了这段代码,但有两个问题。
public class NavigationActivity extends ActionBarActivity implements SensorEventListener {
private Sensor gyroscope;
private SensorManager mSensorManager;
private ImageView go, right, left, back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
go = (ImageView) findViewById(R.id.nav_go);
right = (ImageView) findViewById(R.id.nav_right);
left = (ImageView) findViewById(R.id.nav_left);
back = (ImageView) findViewById(R.id.nav_back);
go.setVisibility(View.GONE);
right.setVisibility(View.GONE);
left.setVisibility(View.GONE);
back.setVisibility(View.GONE);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
gyroscope = (Sensor)mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);}
@Override
protected void onResume() {
super.onResume();
//Register for sensor update
mSensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_UI);}
}
这是OnSensorChanged:http://postimg.org/image/4my837rr9/(在上面相同的代码中)
这是UI:
http://s24.postimg.org/6gfyy2qrp/Stack.png
P.S
1.默认屏幕方向为“横向”
2.我建立的信息(方位角,俯仰和滚动)是根据以下内容:
值[0]:方位角,绕Z轴旋转(0 <=方位角<360)。 0 =北,90 =东,180 =南,270 =西
值[1]:间距,围绕X轴旋转(-180 <=间距<= 180),当z轴向y轴移动时具有正值。
值[2]:滚动,绕Y轴旋转(-90 <=滚动&lt; = 90),当z轴向x轴移动时为正值。