Android 加速度传感器(Accelerometer sensor)
加速度传感器的单位 是 (m/s^2)
方向传感器获取到的加速度是: 手机运动的加速度与重力加速度 (9.81m/s^2) 的合加速度
另外重力加速度是 垂直向下
我们先来看看不同放置位置的加速度值
value 数组的三个值分别对应 X,Y,Z 轴上的加速度
水平放置
竖直平放
竖直横放
简单的 Demo
-
创建一个 空的 Android 项目
cn.twle.android.Accelerometer
-
修改
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/ms_x" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="X"/> <TextView android:id="@+id/ms_y" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Y" /> <TextView android:id="@+id/ms_z" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Z" /> </LinearLayout>
-
修改 MainActivity.java 文件
package cn.twle.android.accelerometer; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements SensorEventListener { private TextView ms_x,ms_y,ms_z; private SensorManager sm; private Sensor mSensorOrientation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取传感器管理器 sm = (SensorManager) getSystemService(SENSOR_SERVICE); // 获取加速度传感器 mSensorOrientation = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //注册数值变化监听器 sm.registerListener(this, mSensorOrientation,SensorManager.SENSOR_DELAY_UI); ms_x = (TextView) findViewById(R.id.ms_x); ms_y = (TextView) findViewById(R.id.ms_y); ms_z = (TextView) findViewById(R.id.ms_z); } // 传感器数值变化会调用此方法 @Override public void onSensorChanged(SensorEvent event) { ms_x.setText("X:" + (float) (Math.round(event.values[0] * 100)) / 100); ms_y.setText("Y:" + (float) (Math.round(event.values[1] * 100)) / 100); ms_z.setText("Z:" + (float) (Math.round(event.values[2] * 100)) / 100); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }
简易计步器
-
创建一个 空的 Android 项目
cn.twle.android.Pedometer
-
修改
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:text="简易计步器" android:textSize="25sp" /> <TextView android:id="@+id/ms_step" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" android:text="0" android:textColor="#DE5347" android:textSize="100sp" android:textStyle="bold" /> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:layout_height="64dp" android:text="开始" android:textSize="25sp" /> </LinearLayout>
-
修改
MainActivity.java
package cn.twle.android.pedometer; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener, SensorEventListener { private SensorManager sm; private Sensor mSensorAccelerometer; private TextView ms_step; private Button btn_start; private int step = 0; //步数 private double oriValue = 0; //原始值 private double lstValue = 0; //上次的值 private double curValue = 0; //当前值 private boolean motiveState = true; //是否处于运动状态 private boolean processState = false; //标记当前是否已经在计步 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sm = (SensorManager) getSystemService(SENSOR_SERVICE); mSensorAccelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sm.registerListener(this, mSensorAccelerometer, SensorManager.SENSOR_DELAY_UI); ms_step = (TextView) findViewById(R.id.ms_step); btn_start = (Button) findViewById(R.id.btn_start); btn_start.setOnClickListener(this); } @Override public void onSensorChanged(SensorEvent event) { //设定一个精度范围 double range = 1; float[] value = event.values; //计算当前的模 curValue = magnitude(value[0], value[1], value[2]); //向上加速的状态 if (motiveState == true) { if (curValue >= lstValue) lstValue = curValue; else { //检测到一次峰值 if (Math.abs(curValue - lstValue) > range) { oriValue = curValue; motiveState = false; } } } //向下加速的状态 if (motiveState == false) { if (curValue <= lstValue) lstValue = curValue; else { if (Math.abs(curValue - lstValue) > range) { //检测到一次峰值 oriValue = curValue; if (processState == true) { step++; //步数 + 1 if (processState == true) { ms_step.setText(step + ""); //读数更新 } } motiveState = true; } } } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} @Override public void onClick(View v) { step = 0; ms_step.setText("0"); if (processState == true) { btn_start.setText("开始"); processState = false; } else { btn_start.setText("停止"); processState = true; } } //向量求模 public double magnitude(float x, float y, float z) { double magnitude = 0; magnitude = Math.sqrt(x * x + y * y + z * z); return magnitude; } @Override protected void onDestroy() { super.onDestroy(); sm.unregisterListener(this); } }