Hoe krijg ik de HUIDIGE oriëntatie (ActivityInfo.SCREEN_ORIENTATION_*) van een Android-apparaat?

Ik zou graag de gedetailleerde oriëntatie van een apparaat willen weten, bij voorkeur een van SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, SCREEN_ORIENTATION_REVERSE_LANDSCAPE, SCREEN_ORIENTATION_REVERSE_PORTRAITvan ActivityInfoof gelijkwaardig.

Enkele antwoorden hier op StackOverflow inbegrepen

getWindowManager().getDefaultDisplay().getRotation()

maar dit vertelt me ​​niet echt of het apparaat in staande of liggende modus staat, alleen hoe het wordt gedraaid met verwijzing naar zijn natuurlijke positie – wat op zijn beurt in de eerste plaats liggend of staand kan zijn.

getResources().getConfiguration().orientation

retourneert een van de volgende drie: ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT, ORIENTATION_SQUARE, die me dan niet echt vertelt hoe de telefoon is gedraaid (of het nu ondersteboven is of naar welke kant het is gedraaid).

Ik weet dat ik de laatste zou kunnen gebruiken in combinatie met DisplayMetricsom de natuurlijke oriëntatie van het apparaat te achterhalen, maar is er echt geen betere manier?


Antwoord 1, autoriteit 100%

Ik heb uiteindelijk de volgende oplossing gebruikt:

private int getScreenOrientation() {
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int orientation;
    // if the device's natural orientation is portrait:
    if ((rotation == Surface.ROTATION_0
            || rotation == Surface.ROTATION_180) && height > width ||
        (rotation == Surface.ROTATION_90
            || rotation == Surface.ROTATION_270) && width > height) {
        switch(rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_180:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            case Surface.ROTATION_270:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            default:
                Log.e(TAG, "Unknown screen orientation. Defaulting to " +
                        "portrait.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;              
        }
    }
    // if the device's natural orientation is landscape or if the device
    // is square:
    else {
        switch(rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_180:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            case Surface.ROTATION_270:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            default:
                Log.e(TAG, "Unknown screen orientation. Defaulting to " +
                        "landscape.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;              
        }
    }
    return orientation;
}

OPMERKING: Sommige gebruikers (Geltrude en holtaf in de opmerkingen hieronder) wezen erop dat deze oplossing niet op alle apparaten werkt, omdat de draairichting vanuit de natuurlijke oriëntatie niet gestandaardiseerd is.


Antwoord 2, autoriteit 23%

Een eenvoudige benadering zou zijn om

. te gebruiken

getResources().getConfiguration().orientation

1 is voor Potrait en 2 voor Landschap.


Antwoord 3, autoriteit 18%

public static int getScreenOrientation(Activity activity) {
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        int orientation = activity.getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
          if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) {
            return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
          } else {
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
          }
        }
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
          if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
          } else {
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
          }
        }
        return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
      }

Antwoord 4, autoriteit 8%

getResources().getConfiguration().orientationis de standaardmanier om te weten of de huidige oriëntatie wordt gebruikt. Als het echter niet aan uw behoeften voldoet, kunt u sensoren gebruiken om het in termen van hoek te berekenen. Lees diten dit


Antwoord 5, autoriteit 8%

Ik denk dat je probleem is dat je landschap en portret kunt detecteren, maar niet omgekeerd landschap en omgekeerd portret, omdat ze niet worden ondersteund in oudere versies. Om te detecteren wat u kunt doen, is dat u zowel oreintatie als rotatie kunt gebruiken. Ik geef je een idee dat het nuttig voor je kan zijn.

probeer dit, ik denk dat dit je probleem kan oplossen.

           int orientation = getResources().getConfiguration().orientation;
            int rotation = getWindowManager().getDefaultDisplay().getRotation();
            int actual_orientation = -1;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE
            &&  (rotation == Surface.ROTATION_0 
            ||  rotation == Surface.ROTATION_90)){
                orientation = Configuration.ORIENTATION_LANDSCAPE;
            } else if (orientation == Configuration.ORIENTATION_PORTRAIT
                  &&  (rotation == Surface.ROTATION_0 
                   ||  rotation == Surface.ROTATION_90)) {
                orientation = Configuration.ORIENTATION_PORTRAIT;
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE
                  &&  (rotation == Surface.ROTATION_180 
                   ||  rotation == Surface.ROTATION_270)){
                orientation = //any constant for reverse landscape orientation;
            } else {
                if (orientation == Configuration.ORIENTATION_PORTRAIT
                        &&  (rotation == Surface.ROTATION_180 
                         ||  rotation == Surface.ROTATION_270)){
                      orientation = //any constant for reverse portrait orientation;
                }
            }

Antwoord 6, autoriteit 2%

Uiteindelijk gebruikte ik het bovenstaande antwoord van Zoltán, wat prima werkt, behalve wanneer ik het op een tablet probeerde (een Samsung P6210 Galaxy Tab 7.0 Plus). In portretmodus retourneerde het SCREEN_ORIENTATION_REVERSE_PORTRAIT. Dus in de else-instructie (als natuurlijke oriëntatie landschap is) heb ik de gevallen verwisseld voor ROTATION_90 en ROTATION_270, en alles lijkt goed te werken. (Ik heb niet genoeg reputatie om dit als commentaar op het antwoord van Zoltán te plaatsen.)


Antwoord 7

Je zou het op een heel eenvoudige manier kunnen doen:
verkrijg het scherm widhten height.
de schermbreedte is altijd groter als het apparaat in de liggende stand staat.

Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth(); 
    int height = display.getHeight();
    Toast.makeText(getApplicationContext(), "" + width + "," + height,
            Toast.LENGTH_SHORT).show();
    if (width > height) {
        Toast.makeText(getApplicationContext(), "LandScape",
                Toast.LENGTH_SHORT).show();
    }

Antwoord 8

Lost dit je probleem op?

public static int getscrOrientation(Activity act)
{
    Display getOrient = act.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 = act.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
}

Other episodes