gpt4 book ai didi

android - 获取 Android 设备的方向

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:18 27 4
gpt4 key购买 nike

您会认为会有一个直接的解决方案。 Android 文档状态:

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() 的解决方案。我花了几个小时阅读了这里关于开发人员使用这些方法的帖子,但他们都有部分粘贴的代码或一些奇怪的实现。谷歌搜索没有提供教程。有人可以粘贴一个使用这两种方法生成方向的简单解决方案吗??

最佳答案

这是 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 definitely be landscape
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
}
}
return orientation; // return value 1 is portrait and 2 is Landscape Mode
}

你也可以引用这个例子,它代表了这两种方法的使用:

     getOrientation and getRotationMatrix

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

关于android - 获取 Android 设备的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14955728/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com