人的偏旁部首是什么:流氓 Android传感器API之:方向SensorOrientation传感器功能实现与源码分享
来源:百度文库 编辑:偶看新闻 时间:2024/05/03 03:11:04
继续明确一下空间坐标系的三个方向:
x 方向就是手机的水平方向,右为正;
y 方向就是手机的水平垂直方向,前为正;
z 方向就是手机的空间垂直方向,天空的方向为正,地球的方向为负。
方向角的定义是手机y轴 水平面上的投影 与 正北方向的夹角。 (值得范围是 0 ~ 359 其中0=North, 90=East, 180=South, 270=West)
倾斜角的定义是手机y轴 与水平面的夹角 (手机z轴向y轴方向移动为正 ,值得范围是 -180 ~ 180)
旋转角的定义是手机x轴 与水平面的夹角 (手机x轴离开z轴方向为正, 值得范围是 -90 ~ 90)
也就是说,当你把手机水平放置在桌面上(屏幕向上)且手机指向正北(Y轴方向),此时传感器获得的xyz三个值应该都为0。
如下是Rexsee实现的方向传感器功能源码。我会把Rexsee扩展的全部传感器源码都陆续贴出来,感兴趣的也可以直接去Rexsee社区查阅,反正都是开源的:http://www.rexsee.com。
但是首先需要知道,并不是所有的Android手机都支持方向传感器。。比如说乐Phone,好像就是加速度、重力和光线,方向和磁场貌似都不支持。
标签: Android SDK Rexsee
x 方向就是手机的水平方向,右为正;
y 方向就是手机的水平垂直方向,前为正;
z 方向就是手机的空间垂直方向,天空的方向为正,地球的方向为负。
方向角的定义是手机y轴 水平面上的投影 与 正北方向的夹角。 (值得范围是 0 ~ 359 其中0=North, 90=East, 180=South, 270=West)
倾斜角的定义是手机y轴 与水平面的夹角 (手机z轴向y轴方向移动为正 ,值得范围是 -180 ~ 180)
旋转角的定义是手机x轴 与水平面的夹角 (手机x轴离开z轴方向为正, 值得范围是 -90 ~ 90)
也就是说,当你把手机水平放置在桌面上(屏幕向上)且手机指向正北(Y轴方向),此时传感器获得的xyz三个值应该都为0。
如下是Rexsee实现的方向传感器功能源码。我会把Rexsee扩展的全部传感器源码都陆续贴出来,感兴趣的也可以直接去Rexsee社区查阅,反正都是开源的:http://www.rexsee.com。
但是首先需要知道,并不是所有的Android手机都支持方向传感器。。比如说乐Phone,好像就是加速度、重力和光线,方向和磁场貌似都不支持。
标签: Android SDK Rexsee
代码片段(1)
[代码] [HTML]代码
001
/*
002
* Copyright (C) 2011 The Rexsee Open Source Project
003
*
004
* Licensed under the Rexsee License, Version 1.0 (the "License");
005
* you may not use this file except in compliance with the License.
006
* You may obtain a copy of the License at
007
*
008
* http://www.rexsee.com/CN/legal/license.html
009
*
010
* Unless required by applicable law or agreed to in writing, software
011
* distributed under the License is distributed on an "AS IS" BASIS,
012
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013
* See the License for the specific language governing permissions and
014
* limitations under the License.
015
*/
016
017
package rexsee.sensor;
018
019
import rexsee.core.browser.JavascriptInterface;
020
import rexsee.core.browser.RexseeBrowser;
021
import android.content.Context;
022
import android.hardware.Sensor;
023
import android.hardware.SensorEvent;
024
import android.hardware.SensorEventListener;
025
import android.hardware.SensorManager;
026
027
public class RexseeSensorOrientation implements JavascriptInterface {
028
029
public static final String INTERFACE_NAME = "Orientation";
030
@Override
031
public String getInterfaceName() {
032
return mBrowser.application.resources.prefix + INTERFACE_NAME;
033
}
034
@Override
035
public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {
036
return this;
037
}
038
@Override
039
public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {
040
return new RexseeSensorOrientation(childBrowser);
041
}
042
043
public static final String EVENT_ONORIENTATIONCHANGED = "onOrientationChanged";
044
045
private final Context mContext;
046
private final RexseeBrowser mBrowser;
047
private final SensorManager mSensorManager;
048
private SensorEventListener mSensorListener;
049
private final Sensor mSensor;
050
051
private int mRate = SensorManager.SENSOR_DELAY_NORMAL;
052
private int mCycle = 100; //milliseconds
053
private int mEventCycle = 100; //milliseconds
054
055
private float mAccuracyX = 0;
056
private float mAccuracyY = 0;
057
private float mAccuracyZ = 0;
058
059
private long lastUpdate = -1;
060
private long lastEvent = -1;
061
062
private float x = -999f;
063
private float y = -999f;
064
private float z = -999f;
065
066
public RexseeSensorOrientation(RexseeBrowser browser) {
067
mContext = browser.getContext();
068
mBrowser = browser;
069
browser.eventList.add(EVENT_ONORIENTATIONCHANGED);
070
071
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
072
073
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
074
mSensorListener = new SensorEventListener() {
075
@Override
076
public void onAccuracyChanged(Sensor sensor, int accuracy) {
077
}
078
@Override
079
public void onSensorChanged(SensorEvent event) {
080
if (event.sensor.getType() != Sensor.TYPE_ORIENTATION) return;
081
long curTime = System.currentTimeMillis();
082
if (lastUpdate == -1 || (curTime - lastUpdate) > mCycle) {
083
lastUpdate = curTime;
084
float lastX = x;
085
float lastY = y;
086
float lastZ = z;
087
x = event.values[SensorManager.DATA_X];
088
y = event.values[SensorManager.DATA_Y];
089
z = event.values[SensorManager.DATA_Z];
090
if (lastEvent == -1 || (curTime - lastEvent) > mEventCycle) {
091
if (
092
(mAccuracyX >= 0 && Math.abs(x - lastX) > mAccuracyX)
093
|| (mAccuracyY >= 0 && Math.abs(y - lastY) > mAccuracyY)
094
|| (mAccuracyZ >= 0 && Math.abs(z - lastZ) > mAccuracyZ)
095
) {
096
lastEvent = curTime;
097
mBrowser.eventList.run(EVENT_ONORIENTATIONCHANGED);
098
}
099
}
100
}
101
}
102
};
103
104
}
105
106
public String getLastKnownX() {
107
return (x == -999) ? "null" : String.valueOf(x);
108
}
109
public String getLastKnownY() {
110
return (y == -999) ? "null" : String.valueOf(y);
111
}
112
public String getLastKnownZ() {
113
return (z == -999) ? "null" : String.valueOf(z);
114
}
115
116
public void setRate(String rate) {
117
mRate = SensorRate.getInt(rate);
118
}
119
public String getRate() {
120
return SensorRate.getString(mRate);
121
}
122
public void setCycle(int milliseconds) {
123
mCycle = milliseconds;
124
}
125
public int getCycle() {
126
return mCycle;
127
}
128
public void setEventCycle(int milliseconds) {
129
mEventCycle = milliseconds;
130
}
131
public int getEventCycle() {
132
return mEventCycle;
133
}
134
public void setAccuracy(float x, float y, float z) {
135
mAccuracyX = x;
136
mAccuracyY = y;
137
mAccuracyZ = z;
138
}
139
public float getAccuracyX() {
140
return mAccuracyX;
141
}
142
public float getAccuracyY() {
143
return mAccuracyY;
144
}
145
public float getAccuracyZ() {
146
return mAccuracyZ;
147
}
148
149
public boolean isReady() {
150
return (mSensor == null) ? false : true;
151
}
152
public void start() {
153
if (isReady()) {
154
mSensorManager.registerListener(mSensorListener, mSensor, mRate);
155
} else {
156
mBrowser.exception(getInterfaceName(), "Orientation sensor is not found.");
157
}
158
}
159
public void stop() {
160
if (isReady()) {
161
mSensorManager.unregisterListener(mSensorListener);
162
}
163
}
164
165
}