• android 指南针


    引用:http://blog.csdn.net/octobershiner/article/details/6641942

     晚上好~今天继续给大家分享一下第二个重要的感应器,其实获取方向本应该很简单的事情,在文章一中看到 有个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[] 是一个旋转矩阵,用来保存磁场和加速度的数据,大家可以理解未加工的方向数据吧

    R通过下面的静态方法获取,这个方法也是用来填充R[]

     

    public static boolean getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)


    解释以下参数,第一个就是我们需要填充的R数组,大小是9

     

                                第二个是是一个转换矩阵,将磁场数据转换进实际的重力坐标中 一般默认情况下可以设置为null

                                第三个是一个大小为3的数组,表示从加速度感应器获取来的数据  在onSensorChanged中

                                第四个是一个大小为3的数组,表示从磁场感应器获取来的数据    在onSensorChanged中

     

    好了基本逻辑就是这样的,下面给大家演示一个简单的测试方向的例子,可以时刻监听用户的方向

     

     

    1. /* 
    2.  * @author octobershiner 
    3.  * 2011 07 28 
    4.  * SE.HIT 
    5.  * 一个演示通过磁场和加速度两个感应器获取方向数据的例子 
    6.  * */  
    7.   
    8.   
    9. package uni.sensor;  
    10.   
    11. import android.app.Activity;  
    12. import android.content.Context;  
    13. import android.hardware.Sensor;  
    14. import android.hardware.SensorEvent;  
    15. import android.hardware.SensorEventListener;  
    16. import android.hardware.SensorManager;  
    17. import android.os.Bundle;  
    18. import android.util.Log;  
    19.   
    20. public class OrientationActivity extends Activity{  
    21.   
    22.     private SensorManager sm;  
    23.     //需要两个Sensor  
    24.     private Sensor aSensor;  
    25.     private Sensor mSensor;  
    26.       
    27.     float[] accelerometerValues = new float[3];  
    28.     float[] magneticFieldValues = new float[3];  
    29.       
    30.     private static final String TAG = "sensor";  
    31.       
    32.     @Override  
    33.     public void onCreate(Bundle savedInstanceState) {  
    34.         // TODO Auto-generated method stub  
    35.         super.onCreate(savedInstanceState);  
    36.         setContentView(R.layout.main);  
    37.   
    38.         sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);  
    39.         aSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);  
    40.         mSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);  
    41.   
    42.         sm.registerListener(myListener, aSensor, SensorManager.SENSOR_DELAY_NORMAL);  
    43.         sm.registerListener(myListener, mSensor,SensorManager.SENSOR_DELAY_NORMAL);  
    44.         //更新显示数据的方法  
    45.         calculateOrientation();  
    46.   
    47.     }  
    48.     //再次强调:注意activity暂停的时候释放  
    49.     public void onPause(){  
    50.         sm.unregisterListener(myListener);  
    51.         super.onPause();  
    52.     }     
    53.       
    54.       
    55.     final SensorEventListener myListener = new SensorEventListener() {  
    56.     public void onSensorChanged(SensorEvent sensorEvent) {  
    57.           
    58.     if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)  
    59.     magneticFieldValues = sensorEvent.values;  
    60.     if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)  
    61.         accelerometerValues = sensorEvent.values;  
    62.     calculateOrientation();  
    63.     }  
    64.     public void onAccuracyChanged(Sensor sensor, int accuracy) {}  
    65.     };  
    66.   
    67.       
    68.     private  void calculateOrientation() {  
    69.           float[] values = new float[3];  
    70.           float[] R = new float[9];  
    71.           SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues);           
    72.           SensorManager.getOrientation(R, values);  
    73.   
    74.           // 要经过一次数据格式的转换,转换为度  
    75.           values[0] = (float) Math.toDegrees(values[0]);  
    76.           Log.i(TAG, values[0]+"");  
    77.           //values[1] = (float) Math.toDegrees(values[1]);  
    78.           //values[2] = (float) Math.toDegrees(values[2]);  
    79.             
    80.           if(values[0] >= -5 && values[0] < 5){  
    81.              Log.i(TAG, "正北");  
    82.           }  
    83.           else if(values[0] >= 5 && values[0] < 85){  
    84.               Log.i(TAG, "东北");  
    85.           }  
    86.           else if(values[0] >= 85 && values[0] <=95){  
    87.               Log.i(TAG, "正东");  
    88.           }  
    89.           else if(values[0] >= 95 && values[0] <175){  
    90.               Log.i(TAG, "东南");  
    91.           }  
    92.           else if((values[0] >= 175 && values[0] <= 180) || (values[0]) >= -180 && values[0] < -175){  
    93.               Log.i(TAG, "正南");  
    94.           }  
    95.           else if(values[0] >= -175 && values[0] <-95){  
    96.               Log.i(TAG, "西南");  
    97.           }  
    98.           else if(values[0] >= -95 && values[0] < -85){  
    99.               Log.i(TAG, "正西");  
    100.           }  
    101.           else if(values[0] >= -85 && values[0] <-5){  
    102.               Log.i(TAG, "西北");  
    103.           }  
    104.         }  
    105.       
    106.   
    107. }  

       实训的时间非常紧张,抽时间写总结感觉很累,但是感觉收获很多,如果有时间的话,也想给大家分享第二种方法,和这种比起来简单很多,其实大家可以完全参考上篇文章中的代码http://blog.csdn.net/octobershiner/article/details/6639040

    只要把其中的两个Sensor。TYPE_ACCELEROMETER改成 Sensor.TYPE_ORIENTATIO就好了,但是今天分享的方法大家最好掌握,这应该是未来android的标准。

     

       Sensor感应器应该就先暂时介绍到这里吧,该看一下进程线程的东西了,其实hardware包中还有个非常重要的类,Camera摄像头,相信大家也听过android扫描器,很强大。以后有时间和大家分享吧。

       接下来的安排 应该是 线程 activity然后是geocode

       话说我也没有个指导老师,一个人对着SDK研究这些,有些累阿~求高人指点。

  • 相关阅读:
    Struts2_模块包含
    Struts2_访问Web元素
    Struts2_简单数据验证
    获取当前日期
    iOS NSMutableArray添加NSInteger元素
    iOS label换行 自适应
    iOS高德地图自定义annotation添加不同图片
    @property(nonatomic) UIViewAutoresizing autoresizingMask;
    HTML
    图片压缩
  • 原文地址:https://www.cnblogs.com/sode/p/2331259.html
Copyright © 2020-2023  润新知