首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android设备的定位

Android设备的定位
EN

Stack Overflow用户
提问于 2013-02-19 10:57:11
回答 3查看 18.2K关注 0票数 7

你会认为会有一个直接的解决方案。Android文档声明:

定位传感器在Android2.2 (API级别8)中被废弃。我们建议您使用getRotationMatrix()方法和getOrientation()方法来计算方位值,而不是使用来自定位传感器的原始数据。

然而,它们并没有提供如何实现getOrientation()getRotationMatrix()的解决方案。我花了几个小时阅读关于使用这些方法的开发人员的文章,但是他们都有部分粘贴的代码或一些奇怪的实现。谷歌还没有提供任何教程。有人能用这两种方法粘贴一个简单的解决方案来生成方向吗??

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-19 11:25:23

下面是getOrientation()的实现:

代码语言:javascript
复制
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 definitely be landscape
                        orientation = Configuration.ORIENTATION_LANDSCAPE;
                    }
                }
            }
        }
        return orientation; // return value 1 is portrait and 2 is Landscape Mode
    }

您还可以参考这个示例,它表示这两种方法的使用:

代码语言:javascript
复制
     getOrientation and getRotationMatrix

http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html

票数 6
EN

Stack Overflow用户

发布于 2014-01-09 09:14:50

代码语言:javascript
复制
 public int getScreenOrientation() {


// Query what the orientation currently really is.
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)                {
    return 1; // Portrait Mode

}else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    return 2;   // Landscape mode
}
return 0;
}
票数 4
EN

Stack Overflow用户

发布于 2014-01-23 19:02:17

代码语言:javascript
复制
protected void onResume() {
    // change the screen orientation
    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        setContentView(R.layout.portrait);
    } else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setContentView(R.layout.landscape);
    } else {
        setContentView(R.layout.oops);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14955728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档