本文源码: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

更多相关文章

  1. 详解Android(安卓)视频播放时停止后台运行的方法
  2. android:screenOrientation属性详解
  3. Android之设置横屏竖屏
  4. Android(安卓)Scroller简介 ---- 界面滚动
  5. android 中文 API- Scroller
  6. android MediaRecorder实现录屏时带录音功能
  7. JobService的使用介绍
  8. 个人自制unity插件"android动态权限自动申请"的用法
  9. 【Orientation】详解Android中的屏幕方向

随机推荐

  1. Android 获取drawable中图片的高度宽度
  2. Android 取得对话框中EditText的字符串
  3. Android GridView
  4. Android动画效果学习
  5. android:开源AsyncHttpClient
  6. Android(安卓)监视文件
  7. android 系统受保护广播
  8. Android TabLayout设置选中状态标题字体
  9. Android之向中国天气网发送GET请求获取JS
  10. Android ListView下滑会报空指针异常