本文源碼:https://github.com/lioilwin/StepOrient
利用Android傳感器-方向和計步組合使用,可以在地圖上記錄人行走的軌跡圖
傳感器類源碼在上兩篇文章中,本文主要是方向和計步組合使用!

一.方向和計步組合使用,記錄軌跡圖
public class MainActivity extends AppCompatActivity implements StepSensorBase.StepCallBack, OrientSensor.OrientCallBack {
private TextView mStepText;
private TextView mOrientText;
private StepView mStepView;
private StepSensorBase mStepSensor; // 計步傳感器
private OrientSensor mOrientSensor; // 方向傳感器
private int mStepLen = 50; // 步長
@Override
public void Step(int stepNum) {
// 計步回調(diào)
mStepText.setText("步數(shù):" + stepNum);
mStepView.autoAddPoint(mStepLen);
}
@Override
public void Orient(int orient) {
// 方向回調(diào)
mOrientText.setText("方向:" + orient);
// 獲取手機轉(zhuǎn)動停止后的方向
// orient = SensorUtil.getInstance().getRotateEndOrient(orient);
mStepView.autoDrawArrow(orient);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SensorUtil.getInstance().printAllSensor(this); // 打印所有可用傳感器
setContentView(R.layout.activity_main);
mStepText = (TextView) findViewById(R.id.step_text);
mOrientText = (TextView) findViewById(R.id.orient_text);
mStepView = (StepView) findViewById(R.id.step_surfaceView);
// 注冊計步監(jiān)聽
// mStepSensor = new StepSensorPedometer(this, this);
// if (!mStepSensor.registerStep()) {
mStepSensor = new StepSensorAcceleration(this, this);
if (!mStepSensor.registerStep()) {
Toast.makeText(this, "計步功能不可用!", Toast.LENGTH_SHORT).show();
}
// }
// 注冊方向監(jiān)聽
mOrientSensor = new OrientSensor(this, this);
if (!mOrientSensor.registerOrient()) {
Toast.makeText(this, "方向功能不可用!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// 注銷傳感器監(jiān)聽
mStepSensor.unregisterStep();
mOrientSensor.unregisterOrient();
}
}
二.獲取手機轉(zhuǎn)動停止的方向,優(yōu)化轉(zhuǎn)動角度偏差
/**
* 傳感器工具類,
*/
public class SensorUtil {
private static final String TAG = "SensorUtil";
private static final SensorUtil sensorUtil = new SensorUtil(); // 單例常量
private SensorManager sensorManager;
private static final int SENSE = 10; // 方向差值靈敏度
private static final int STOP_COUNT = 6; // 停止次數(shù)
private int initialOrient = -1; // 初始方向
private int endOrient = -1; // 轉(zhuǎn)動停止方向
private boolean isRotating = false; // 是否正在轉(zhuǎn)動
private int lastDOrient = 0; // 上次方向與初始方向差值
private Stack<Integer> dOrientStack = new Stack<>(); // 歷史方向與初始方向的差值棧
···········省略··········
/**
* 獲取手機轉(zhuǎn)動停止的方向
* @param orient 手機實時方向
*/
public int getRotateEndOrient(int orient) {
if (initialOrient == -1) {
// 初始化轉(zhuǎn)動
endOrient = initialOrient = orient;
Log.i(TAG, "getRotateEndOrient: 初始化,方向:" + initialOrient);
}
int currentDOrient = Math.abs(orient - initialOrient); // 當前方向與初始方向差值
if (!isRotating) {
// 檢測是否開始轉(zhuǎn)動
lastDOrient = currentDOrient;
if (lastDOrient >= SENSE) {
// 開始轉(zhuǎn)動
isRotating = true;
}
} else {
// 檢測是否停止轉(zhuǎn)動
if (currentDOrient <= lastDOrient) {
// 至少累計STOP_COUNT次出現(xiàn)當前方向差小于上次方向差
int size = dOrientStack.size();
if (size >= STOP_COUNT) {
// 只有以前SENSE次方向差距與當前差距的差值都小于等于SENSE,才判斷為停止
for (int i = 0; i < size; i++) {
if (Math.abs(currentDOrient - dOrientStack.pop()) >= SENSE) {
isRotating = true;
break;
}
isRotating = false;
}
}
if (!isRotating) {
// 停止轉(zhuǎn)動
dOrientStack.clear();
initialOrient = -1;
endOrient = orient;
Log.i(TAG, "getRotateEndOrient: ------停止轉(zhuǎn)動,方向:" + endOrient);
} else {
// 正在轉(zhuǎn)動,把當前方向與初始方向差值入棧
dOrientStack.push(currentDOrient);
Log.i(TAG, "getRotateEndOrient: 正在轉(zhuǎn)動,方向:" + orient);
}
} else {
lastDOrient = currentDOrient;
}
}
return endOrient;
}
}
簡書: http://www.itdecent.cn/p/06343a6aa8df
CSDN博客: http://blog.csdn.net/qq_32115439/article/details/62961016
GitHub博客:http://c.lioil.win/2017/03/17/Android-Route.html
Coding博客:http://lioil.win/2017/03/17/Android-Route.html