检查Android手机的方向

Moh*_*nde 396 java android orientation

如何检查Android手机是横向还是纵向?

hac*_*bod 645

用于确定要检索哪些资源的当前配置可从Resources的Configuration对象获得:

getResources().getConfiguration().orientation;
Run Code Online (Sandbox Code Playgroud)

您可以通过查看其值来检查方向:

int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
    // In landscape
} else {
    // In portrait
}
Run Code Online (Sandbox Code Playgroud)

可以在Android Developer中找到更多信息.

  • 没有什么可恼火的.屏幕没有旋转,它仍然是纵向,没有旋转看.如果您想监控用户移动手机的方式,无论屏幕如何旋转,那么您需要直接观察传感器,并决定如何解读有关设备移动方式的信息. (13认同)
  • 如果活动锁定显示(`android:screenOrientation ="portrait"`),则此方法将返回相同的值,而不管用户如何旋转设备.在这种情况下,您可以使用加速度计或重力传感器来正确地确定方向. (7认同)
  • 如果屏幕方向固定,则会失败. (4认同)
  • 哦对不起,我误解了,我以为你说如果配置改变,服务不会看到配置改变.你所描述的是......好吧,它没有看到任何东西,因为没有任何变化,因为发射器锁定了屏幕方向并且不允许它改变.所以.orientation不会改变是正确的,因为方向没有改变.屏幕仍然是人像. (2认同)

Jar*_*iuk 170

如果在某些设备上使用getResources().getConfiguration().orientation,则会出错.我们最初在http://apphance.com中使用了这种方法.感谢Apphance的远程记录,我们可以在不同的设备上看到它,我们看到碎片在这里发挥作用.我看到奇怪的情况:例如在HTC Desire HD上交替纵向和方形(?!):

CONDITION[17:37:10.345] screen: rotation: 270 orientation: square
CONDITION[17:37:12.774] screen: rotation: 0 orientation: portrait
CONDITION[17:37:15.898] screen: rotation: 90
CONDITION[17:37:21.451] screen: rotation: 0
CONDITION[17:38:42.120] screen: rotation: 270 orientation: square
Run Code Online (Sandbox Code Playgroud)

或根本不改变方向:

CONDITION[11:34:41.134] screen: rotation: 0
CONDITION[11:35:04.533] screen: rotation: 90
CONDITION[11:35:06.312] screen: rotation: 0
CONDITION[11:35:07.938] screen: rotation: 90
CONDITION[11:35:09.336] screen: rotation: 0
Run Code Online (Sandbox Code Playgroud)

另一方面,width()和height()总是正确的(窗口管理器使用它,所以最好是).我想说最好的想法是进行宽度/高度检查.如果你想一下,这正是你想要的 - 知道宽度是否小于高度(肖像),相反(横向)或它们是否相同(正方形).

然后它归结为这个简单的代码:

public int getScreenOrientation()
{
    Display getOrient = getWindowManager().getDefaultDisplay();
    int orientation = Configuration.ORIENTATION_UNDEFINED;
    if(getOrient.getWidth()==getOrient.getHeight()){
        orientation = Configuration.ORIENTATION_SQUARE;
    } else{ 
        if(getOrient.getWidth() < getOrient.getHeight()){
            orientation = Configuration.ORIENTATION_PORTRAIT;
        }else { 
             orientation = Configuration.ORIENTATION_LANDSCAPE;
        }
    }
    return orientation;
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!然而,初始化"方向"是多余的. (3认同)
  • @ user3441905,是的.请使用`getSize(Point outSize)`.我正在使用API​​ 23. (3认同)

Pau*_*aul 52

解决此问题的另一种方法是不依赖于显示器的正确返回值,而是依赖于Android资源解析.

在文件layouts.xml夹中创建文件res/values-landres/values-port使用以下内容:

RES /价值观土地/ layouts.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="is_landscape">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

RES /值端口/ layouts.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="is_landscape">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

在源代码中,您现在可以按如下方式访问当前方向:

context.getResources().getBoolean(R.bool.is_landscape)
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个,因为它使用系统已经确定方向的任何方式 (2认同)
  • 它在默认值文件中的值是多少? (2认同)

Ngu*_*inh 46

一种指定手机当前方向的完整方式:

    public String getRotation(Context context){
    final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
           switch (rotation) {
            case Surface.ROTATION_0:
                return "portrait";
            case Surface.ROTATION_90:
                return "landscape";
            case Surface.ROTATION_180:
                return "reverse portrait";
            default:
                return "reverse landscape";
            }
        }
Run Code Online (Sandbox Code Playgroud)

Chear Binh Nguyen

  • 你的帖子中有一个拼写错误 - 应该说.getRotation()不是getOrientation (6认同)
  • @Keith @Paul我不记得`getOrientation()`是如何工作的,但如果使用`getRotation()`这是不正确的.获取旋转""从"自然"方向返回屏幕的旋转."[source](http://developer.android.com/reference/android/view/Display.html#getRotation()).所以在手机上说ROTATION_0是肖像可能是正确的,但在平板电脑上它的"自然"方向可能是风景,而ROTATION_0应该返回横向而不是肖像. (6认同)

Ngu*_*Dat 27

以下是hackbodMartijn推荐的代码片段演示如何获取屏幕方向:

❶更改时触发方向:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
        int nCurrentOrientation = _getScreenOrientation();
    _doSomeThingWhenChangeOrientation(nCurrentOrientation);
}
Run Code Online (Sandbox Code Playgroud)

❷获取hackbod推荐的当前方向:

private int _getScreenOrientation(){    
    return getResources().getConfiguration().orientation;
}
Run Code Online (Sandbox Code Playgroud)

❸有获得当前屏幕方向的替代解决方案❷遵循Martijn解决方案:

private int _getScreenOrientation(){
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        return display.getOrientation();
}
Run Code Online (Sandbox Code Playgroud)

注意:我试过两个工具❷&❸,但在RealDevice(NexusOne SDK 2.3)方向上,它返回错误的方向.

★所以我建议使用解决方案 ❷来获得更多优势的屏幕方向:清晰,简单,工作就像一个魅力.

★仔细检查方向的返回,以确保我们的预期正确(可能有限取决于物理设备规格)

希望它有所帮助,

  • 嗨,你的意思是1和3没有返回正确吗? (2认同)

小智 16

int ot = getResources().getConfiguration().orientation;
switch(ot)
        {

        case  Configuration.ORIENTATION_LANDSCAPE:

            Log.d("my orient" ,"ORIENTATION_LANDSCAPE");
        break;
        case Configuration.ORIENTATION_PORTRAIT:
            Log.d("my orient" ,"ORIENTATION_PORTRAIT");
            break;

        case Configuration.ORIENTATION_SQUARE:
            Log.d("my orient" ,"ORIENTATION_SQUARE");
            break;
        case Configuration.ORIENTATION_UNDEFINED:
            Log.d("my orient" ,"ORIENTATION_UNDEFINED");
            break;
            default:
            Log.d("my orient", "default val");
            break;
        }
Run Code Online (Sandbox Code Playgroud)


net*_*ein 13

使用getResources().getConfiguration().orientation它是正确的方式.

您只需要注意不同类型的景观,即设备通常使用的景观和其他景观.

仍然不明白如何管理.


Baz*_*Baz 12

一段时间过去了,因为大多数答案已经发布,有些时候已经弃用了方法和常量.

我已经更新了Jarek的代码,不再使用这些方法和常量了:

protected int getScreenOrientation()
{
    Display getOrient = getWindowManager().getDefaultDisplay();
    Point size = new Point();

    getOrient.getSize(size);

    int orientation;
    if (size.x < size.y)
    {
        orientation = Configuration.ORIENTATION_PORTRAIT;
    }
    else
    {
        orientation = Configuration.ORIENTATION_LANDSCAPE;
    }
    return orientation;
}
Run Code Online (Sandbox Code Playgroud)

请注意,Configuration.ORIENTATION_SQUARE不再支持该模式.

我发现这在我测试过的所有设备上都是可靠的,与建议使用的方法形成鲜明对比 getResources().getConfiguration().orientation


Kum*_*mar 6

在运行时检查屏幕方向.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();        
    }
}
Run Code Online (Sandbox Code Playgroud)


Rez*_*rim 6

只需简单的两行代码

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    // do something in landscape
} else {
    //do in potrait
}
Run Code Online (Sandbox Code Playgroud)


max*_*mus 5

还有一种方法:

public int getOrientation()
{
    if(getResources().getDisplayMetrics().widthPixels>getResources().getDisplayMetrics().heightPixels)
    { 
        Toast t = Toast.makeText(this,"LANDSCAPE",Toast.LENGTH_SHORT);
        t.show();
        return 1;
    }
    else
    {
        Toast t = Toast.makeText(this,"PORTRAIT",Toast.LENGTH_SHORT);
        t.show();
        return 2;
    }       
}
Run Code Online (Sandbox Code Playgroud)


小智 5

Android SDK 可以很好地告诉您这一点:

getResources().getConfiguration().orientation
Run Code Online (Sandbox Code Playgroud)


小智 5

这样就覆盖了所有手机,例如oneplus3

public static boolean isScreenOrientationPortrait(Context context) {
    return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
}
Run Code Online (Sandbox Code Playgroud)

正确代码如下:

public static int getRotation(Context context) {
    final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();

    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
        return Configuration.ORIENTATION_PORTRAIT;
    }

    if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
        return Configuration.ORIENTATION_LANDSCAPE;
    }

    return -1;
}
Run Code Online (Sandbox Code Playgroud)


Man*_*lTS 5

2019 年在 API 28 上进行了测试,无论用户是否设置了纵向方向,与另一个过时的答案相比,使用最少的代码,以下内容提供了正确的方向:

/** @return The {@link Configuration#ORIENTATION_SQUARE}, {@link Configuration#ORIENTATION_PORTRAIT}, {@link Configuration#ORIENTATION_LANDSCAPE} constants based on the current phone screen pixel relations. */
private int getScreenOrientation()
{
    DisplayMetrics dm = context.getResources().getDisplayMetrics(); // Screen rotation effected

    if(dm.widthPixels == dm.heightPixels)
        return Configuration.ORIENTATION_SQUARE;
    else
        return dm.widthPixels < dm.heightPixels ? Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
}
Run Code Online (Sandbox Code Playgroud)