方向传感器
在安卓平台上,传感器通常使用三维坐标系来确定方向,这个坐标系是一个数值,通过获取该数值便可以确定所处的方向,,系统返回的值时长度为三的float数组,包含三个方向的值(X,Y,Z)
1.如何获取坐标系的值
通过onSensorChanged中的参数SensorEvent event,即可获取坐标系的值,event值是float[]类型,而且最多有三个参数,这三个对应关系如下
下面的操作都是手机朝上水平放在桌面上,values=0
- values[0]:方位角,手机绕着
Z
轴旋转的角度,0-正北(North),90-正东(East),180-正南(South),270-正西(West) - values[1]:倾斜角,手机翘起来的程度,手机绕着
X
周旋转,取值[ -180 ~~ +180 ],手机顶部翻到桌面(0~ -180),底部翻到桌面(0~+180) - values[2]:滚动角,沿着
Y
轴的滚动角度,取值[ -90 ~~ +90 ],从左侧抬起到垂直桌面[0 ~~ -90 ],右侧抬起到垂直[ 0 ~~ +90 ],在垂直位置继续转动,值还在[ -90 ~~ +90 ]之间变化
2.实例,获取手机中坐标系的值
①在xml文件里设置TextView组件输出值
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#0000ff"
android:textSize="40sp"
/><