当前位置:   article > 正文

Android中陀螺仪的使用_android陀螺仪监听位置变化

android陀螺仪监听位置变化

陀螺仪顾名思义是测量平衡的仪器,它的测量结果为当前与上次位置之间的倾斜角度,这个角度描述的是三维空间上的夹角,因而其数值由x,y,z三个坐标轴上的角度偏移组成。由于陀螺仪具备三维角度的测量功能,因此它又被称作角速度传感器。陀螺仪能够检测旋转角度的大小,所以利用陀螺仪可以还原三维物体的转动行为。

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:id="@+id/tv_light"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:padding="5dp"
  11. android:text=""
  12. android:textColor="@color/black"
  13. android:textSize="17sp" />
  14. </LinearLayout>
MainActivity.java
  1. public class MainActivity extends BaseActivity
  2. implements SensorEventListener {
  3. private TextView tv_light;
  4. private SensorManager mSensorMgr;// 声明一个传感管理器对象
  5. private float mTimestamp; // 记录上次的时间戳
  6. private float mAngle[] = new float[3]; // 记录xyz三个方向上的旋转角度
  7. private static final float NS2S = 1.0f / 1000000000.0f; // 将纳秒转化为秒
  8. @Override
  9. protected MvcBaseModel getModelImp() {
  10. return null;
  11. }
  12. @Override
  13. protected int getContentLayoutId() {
  14. return R.layout.activity_main;
  15. }
  16. @Override
  17. protected void initWidget() {
  18. tv_light = findViewById(R.id.tv_light);
  19. // 从系统服务中获取传感管理器对象
  20. mSensorMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  21. }
  22. @Override
  23. protected void onPause() {
  24. super.onPause();
  25. // 注销当前活动的传感监听器
  26. mSensorMgr.unregisterListener(this);
  27. }
  28. @Override
  29. protected void onResume() {
  30. super.onResume();
  31. //注册感光器
  32. mSensorMgr.registerListener(this,
  33. mSensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
  34. SensorManager.SENSOR_DELAY_NORMAL);
  35. }
  36. @Override
  37. public void onSensorChanged(SensorEvent event) {
  38. if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) { // 陀螺仪角度变更事件
  39. if (mTimestamp != 0) {
  40. final float dT = (event.timestamp - mTimestamp) * NS2S;
  41. mAngle[0] += event.values[0] * dT;
  42. mAngle[1] += event.values[1] * dT;
  43. mAngle[2] += event.values[2] * dT;
  44. // x轴的旋转角度,手机平放桌上,然后绕侧边转动
  45. float angleX = (float) Math.toDegrees(mAngle[0]);
  46. // y轴的旋转角度,手机平放桌上,然后绕底边转动
  47. float angleY = (float) Math.toDegrees(mAngle[1]);
  48. // z轴的旋转角度,手机平放桌上,然后水平旋转
  49. float angleZ = (float) Math.toDegrees(mAngle[2]);
  50. String desc = String.format("陀螺仪检测到当前\nx轴方向的转动角度为%f\ny轴方向的转动角度为%f\nz轴方向的转动角度为%f",
  51. angleX, angleY, angleZ);
  52. tv_light.setText(desc);
  53. }
  54. mTimestamp = event.timestamp;
  55. }
  56. }
  57. //当传感器精度改变时回调该方法,一般无需处理
  58. public void onAccuracyChanged(Sensor sensor, int accuracy) {}
  59. }

这样我们就知道手机围绕哪个轴进行了转动操作。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/746219
推荐阅读
相关标签