当前位置:操作系统 > 安卓/Android >>

Android传感器之方向传感器Orientation功能实现与源码

先明确一下空间坐标系的三个方向:
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对于方向传感器Orientation的扩展,我们可以很快的实现传感器功能,比如读取X/Y/Z方向角度值:rexseeOrientation.getLastKnownX();读取响应速度:rexseeOrientation.getRate();设置传感器检测周期:rexseeOrientation.setCycle(1000)等。具体的函数介绍可以在Rexsee社区的手册与源码中查阅:http://www.rexsee.com/CN/helpReference.php

如下是Rexsee用Java封装的的方向传感器功能详细源码。感兴趣的也可以直接去Rexsee社区查阅:http://www.rexsee.com。
但是首先需要知道,并不是所有的Android手机都支持方向传感器。。比如说乐Phone,好像就是加速度、重力和光线,方向和磁场貌似都不支持。

[java]
/* 
* Copyright (C) 2011 The Rexsee Open Source Project 

* Licensed under the Rexsee License, Version 1.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 

*      http://www.rexsee.com/CN/legal/license.html 

* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/  
  
package rexsee.sensor;   
  
import rexsee.core.browser.JavascriptInte易做图ce;   
import rexsee.core.browser.RexseeBrowser;   
import android.content.Context;   
import android.hardware.Sensor;   
import android.hardware.SensorEvent;   
import android.hardware.SensorEventListener;   
import android.hardware.SensorManager;   
  
public class RexseeSensorOrientation implements JavascriptInte易做图ce {   
  
       public static final String INTERFACE_NAME = "Orientation";   
       @Override   
       public String getInte易做图ceName() {   
               return mBrowser.application.resources.prefix + INTERFACE_NAME;   
       }   
       @Override   
       public JavascriptInte易做图ce getInheritInte易做图ce(RexseeBrowser childBrowser) {   
               return this;   
       }   
       @Override   
       public JavascriptInte易做图ce getNewInte易做图ce(RexseeBrowser childBrowser) {   
               return new RexseeSensorOrientation(childBrowser);   
       }   
  
       public static final String EVENT_ONORIENTATIONCHANGED = "onOrientationChanged";   
  
       private final Context mContext;   
       private final RexseeBrowser mBrowser;   
       private final SensorManager mSensorManager;   
       private SensorEventListener mSensorListener;   
       private final Sensor mSensor;   
  
       private int mRate = SensorManager.SENSOR_DELAY_NORMAL;   
       private int mCycle = 100; //milliseconds    
       private int mEventCycle = 100; //milliseconds    
  
       private float mAccuracyX = 0;   
       private float mAccuracyY = 0;   
       private float mAccuracyZ = 0;   
  
       private long lastUpdate = -1;   
       private long lastEvent = -1;   
  
       private float x = -999f;   
       private float y = -999f;   
       private float z = -999f;   
  
       public RexseeSensorOrientation(RexseeBrowser browser) {   
               mContext = browser.getContext();   
               mBrowser = browser;   
               browser.eventList.add(EVENT_ONORIENTATIONCHANGED);   
  
               mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);   
  
               mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);   
               mSensorListener = new SensorEventListener() {   
                       @Override   
                       public void onAccuracyChanged(Sensor sensor, int accuracy) {   
                       }   
                       @Override   
                       public void onSensorChanged(SensorEvent event) {   
           &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,