本文源碼: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) {
// 計步回調
mStepText.setText("步數:" + stepNum);
mStepView.autoAddPoint(mStepLen);
}
@Override
public void Orient(int orient) {
// 方向回調
mOrientText.setText("方向:" + orient);
// 擷取手機轉動停止後的方向
// 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);
// 注冊計步監聽
// mStepSensor = new StepSensorPedometer(this, this);
// if (!mStepSensor.registerStep()) {
mStepSensor = new StepSensorAcceleration(this, this);
if (!mStepSensor.registerStep()) {
Toast.makeText(this, "計步功能不可用!", Toast.LENGTH_SHORT).show();
}
// }
// 注冊方向監聽
mOrientSensor = new OrientSensor(this, this);
if (!mOrientSensor.registerOrient()) {
Toast.makeText(this, "方向功能不可用!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// 登出傳感器監聽
mStepSensor.unregisterStep();
mOrientSensor.unregisterOrient();
}
}
二.擷取手機轉動停止的方向,優化轉動角度偏差
/**
* 傳感器工具類,
*/
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; // 停止次數
private int initialOrient = -1; // 初始方向
private int endOrient = -1; // 轉動停止方向
private boolean isRotating = false; // 是否正在轉動
private int lastDOrient = 0; // 上次方向與初始方向內插補點
private Stack<Integer> dOrientStack = new Stack<>(); // 曆史方向與初始方向的內插補點棧
···········省略··········
/**
* 擷取手機轉動停止的方向
* @param orient 手機實時方向
*/
public int getRotateEndOrient(int orient) {
if (initialOrient == -1) {
// 初始化轉動
endOrient = initialOrient = orient;
Log.i(TAG, "getRotateEndOrient: 初始化,方向:" + initialOrient);
}
int currentDOrient = Math.abs(orient - initialOrient); // 目前方向與初始方向內插補點
if (!isRotating) {
// 檢測是否開始轉動
lastDOrient = currentDOrient;
if (lastDOrient >= SENSE) {
// 開始轉動
isRotating = true;
}
} else {
// 檢測是否停止轉動
if (currentDOrient <= lastDOrient) {
// 至少累計STOP_COUNT次出現目前方向差小于上次方向差
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) {
// 停止轉動
dOrientStack.clear();
initialOrient = -1;
endOrient = orient;
Log.i(TAG, "getRotateEndOrient: ------停止轉動,方向:" + endOrient);
} else {
// 正在轉動,把目前方向與初始方向內插補點入棧
dOrientStack.push(currentDOrient);
Log.i(TAG, "getRotateEndOrient: 正在轉動,方向:" + orient);
}
} else {
lastDOrient = currentDOrient;
}
}
return endOrient;
}
}
簡書: http://www.jianshu.com/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