没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论











Android使用使用Sensor感应器获取用户移动方向感应器获取用户移动方向(指南针原理指南针原理)
主要介绍了Android使用Sensor感应器获取用户移动方向的方法,实例分析了指南针原理极其应用,需要的朋友可
以参考下
本文实例讲述了Android使用Sensor感应器获取用户移动方向的方法。分享给大家供大家参考,具体如下:
今天继续给大家分享一下第二个重要的感应器,其实获取方向本应该很简单的事情,在前面文章中看到有个
TYPE_ORIENTATION 关键字,说明可以直接获取设备的移动方向,但是最新版的SDK加上了这么一句
话“TYPE_ORIENTATION This constant is deprecated. use SensorManager.getOrientation() instead. ”也就是说,这种方式
已经被取消,要开发者使用 SensorManager.getOrientation()来获取原来的数据。
实际上,android获取方向是通过磁场感应器和加速度感应器共同获得的,至于具体的算法SDK已经封装好了。也就是说现在
获取用户方向有两种方式,一是官方推荐的,通过SensorManager.getOrientation()来获取,这个方法表面看似容易(那是因
为你还没看到他的参数。。一会再说),但实际上需要用到两个感应器共同完成工作,特点是更加的准确。第二种方法非常简
单,就像前一篇文章获取加速度一样,直接得到三个轴上的数据。
额,从难一些的介绍吧,因为毕竟第一种方法会是android未来的一个选择,第二种不知道什么时候就要成为历史了。
android给我们提供的方向数据是一个float型的数组,包含三个方向的值 如图
当你的手机水平放置时,被默认为静置状态,即XY角度均为0
values[0] 表示Z轴的角度:方向角,我们平时判断的东西南北就是看这个数据的,经过我的实验,发现了一个有意思的事情,
也就是说使用第一种方式获得方向(磁场+加速度)得到的数据范围是(-180~180),也就是说,0表示正北,90表示正
东,180/-180表示正南,-90表示正西。而第二种方式(直接通过方向感应器)数据范围是(0~360)360/0表示正北,90表
示正东,180表示正南,270表示正西。
values[1] 表示X轴的角度:俯仰角 即由静止状态开始,前后翻转
values[2] 表示Y轴的角度:翻转角 即由静止状态开始,左右翻转
可见统一获取方向的方法是必须的,因为处理这些数据的算法可能针对第一种获取方式,那么当用在第二种方式时,移植性就
不好了。
看下面的方法
public static float[] getOrientation (float[] R, float[] values)
Since: API Level 3
Computes the device's orientation based on the rotation matrix.
When it returns, the array values is filled with the result:
values[0]: azimuth, rotation around the Z axis.
values[1]: pitch, rotation around the X axis.
values[2]: roll, rotation around the Y axis.
The reference coordinate-system used is different from the world coordinate-system defined for the rotation matrix:
X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points
West).
Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.
Z points towards the center of the Earth and is perpendicular to the ground.
All three angles above are in radians and positive in the counter-clockwise direction.
通常我们并不需要获取这个函数的返回值,这个方法会根据参数R[]的数据填充values[]而后者就是我们想要的。
那么那么R表示什么呢?又将怎么获取呢?表示什么呢?又将怎么获取呢?
R[] 是一个旋转矩阵,用来保存磁场和加速度的数据,大家可以理解未加工的方向数据吧
资源评论


weixin_38662213
- 粉丝: 3
- 资源: 916
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
