的Andr​​oid设备获取定位 [英] Getting orientation of Android device

查看:124
本文介绍了的Andr​​oid设备获取定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能会认为将有一个简单的解决方案。 Android的文档状态:

You would think that there would be a straight forward solution. The Android docs state:

该方向传感器是pcated中的Andr​​oid 2.2(API级别8)日$ P $。   而不是使用来自方位传感器的原始数据,我们建议   您在结合使用getRotationMatrix()方法,   getOrientation()方法来计算方向值。

The orientation sensor was deprecated in Android 2.2 (API level 8). Instead of using raw data from the orientation sensor, we recommend that you use the getRotationMatrix() method in conjunction with the getOrientation() method to compute orientation values.

然而,他们没有提供关于如何实现解决方案 getOrientation() getRotationMatrix()。我花了几个小时,通过职位在这里阅读开发人员使用这些方法,但他们都部分地粘贴code或一些怪异的实现。谷歌搜索并没有提供一个教程。是否有人可以用这两种方法生成的方向?粘贴一个简单的解决方案

Yet, they don't provide a solution on how to implement getOrientation() and getRotationMatrix(). I've spent several hours reading through posts here on developers using these methods but they all have partially pasted code or some weird implementation. Googling hasn't provided a tutorial. Can someone please paste a simple solution using these two methods to generate the orientation??

推荐答案

这里是实现getOrientation():

here is the implementation for getOrientation() :

public int getscrOrientation()
    {
        Display getOrient = getWindowManager().getDefaultDisplay();

        int orientation = getOrient.getOrientation();

        // Sometimes you may get undefined orientation Value is 0
        // simple logic solves the problem compare the screen
        // X,Y Co-ordinates and determine the Orientation in such cases
        if(orientation==Configuration.ORIENTATION_UNDEFINED){

            Configuration config = getResources().getConfiguration();
            orientation = config.orientation;

            if(orientation==Configuration.ORIENTATION_UNDEFINED){
                //if height and widht of screen are equal then
                // it is square orientation
                if(getOrient.getWidth()==getOrient.getHeight()){
                    orientation = Configuration.ORIENTATION_SQUARE;
                }else{ //if widht is less than height than it is portrait
                    if(getOrient.getWidth() < getOrient.getHeight()){
                        orientation = Configuration.ORIENTATION_PORTRAIT;
                    }else{ // if it is not any of the above it will defineitly be landscape
                        orientation = Configuration.ORIENTATION_LANDSCAPE;
                    }
                }
            }
        }
        return orientation; // return value 1 is portrait and 2 is Landscape Mode
    }

和U也可以参照这个例子再present使用两种方法:

And u can also refer this example which represent the use of both the methods :

     getOrientation and getRotationMatrix

<一个href="http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html">http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html

这篇关于的Andr​​oid设备获取定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆