2012-04-30 11 views
52

Ich würde gerne die detaillierte Ausrichtung eines Gerätes, vorzugsweise eines SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, SCREEN_ORIENTATION_REVERSE_LANDSCAPE, SCREEN_ORIENTATION_REVERSE_PORTRAIT von ActivityInfo oder gleichwertig herausfinden.Wie bekomme ich die CURRENT-Ausrichtung (ActivityInfo.SCREEN_ORIENTATION_ *) eines Android-Geräts?

Einige der Antworten hier auf Stackoverflow enthalten

getWindowManager().getDefaultDisplay().getRotation() 

aber das hat mich wirklich nicht sagen, ob das Gerät im Hoch- oder Querformat ist, wie es nur in Bezug auf seine natürliche Position gedreht wird - was in drehen kann Landschaft oder Porträt an erster Stelle sein.

getResources().getConfiguration().orientation 

kehrt eine der folgenden drei: ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT, ORIENTATION_SQUARE, die mir wirklich nicht, dann nicht sagen, welche Art und Weise das Telefon eingeschaltet wird (ob es auf dem Kopf ist oder der von den Seiten es gedreht).

Ich weiß, ich könnte letzteres in Kombination mit DisplayMetrics verwenden, um herauszufinden, die natürliche Ausrichtung des Geräts, aber gibt es wirklich keinen besseren Weg?

Antwort

85

Ich landete mit der folgenden Lösung:

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; 
} 

HINWEIS: Einige Benutzer (Geltrude und holtaf in den Kommentaren unten) wies darauf hin, dass diese Lösung nicht auf allen Geräten, wie die Arbeit Drehrichtung aus der natürlichen Ausrichtung ist nicht standardisiert.

+1

Super! Vielen Dank! – Alexander

+1

Funktioniert brilliant! –

+3

Dies schlägt fehl, wenn Ihre Aktivität eine feste Bildschirmausrichtung hat. getRotation meldet nur einen Wert, wenn die Bildschirmausrichtung nicht gesperrt ist. – AndroidDev

7

getResources().getConfiguration().orientation ist die Standardmethode zur Kenntnis der aktuellen Ausrichtung. Wenn es jedoch Ihren Anforderungen nicht entspricht, können Sie möglicherweise Sensoren verwenden, um es in Bezug auf den Winkel zu berechnen. Lesen Sie this und this

+0

Ja, ich bin mir dessen bewusst. Ich wollte nur wissen, ob es eine einzige Methode gibt, die mir vollständige Informationen über die Ausrichtung des Bildschirms liefert, einschließlich der Seite, auf die er gerichtet ist (nicht nur im Querformat oder im Hochformat), d.h. auf dem Kopf oder links oder rechts. –

+0

Ja, ich habe Ihren Standpunkt verstanden. Also, was Sie tun können, ist Ihre eigene einzige statische Methode mit diesen Sensoren zu machen ... – waqaslam

-1

Löst dies Ihr Problem?

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 
} 
+1

Wie ich schon sagte in meiner Frage. Ich muss wissen, auf welche Weise der Bildschirm gedreht wird. Portrait oder Landschaft sind nicht ausreichend Info. Z.B. Wenn es ein Hoch- oder Hochformat ist, wenn es im Querformat ausgerichtet ist, muss ich wissen, ob es auf der linken oder rechten Seite eingeschaltet ist usw. –

7

Ich denke, Ihr Problem ist, dass Sie Landschafts- und Portrait erkennen können, aber nicht umkehren Landschaft und Reverse protrait, wie sie in älteren Versionen nicht unterstützt werden. Um festzustellen, was Sie tun können, können Sie sowohl die Ausrichtung als auch die Rotation verwenden. Ich gebe Ihnen eine Idee, die für Sie nützlich sein könnte.

versuchen Sie dies, ich denke, es kann Ihr Problem lösen.

  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; 
       } 
      } 
+0

Ja, ich habe eine ähnliche Lösung gefunden. Ich habe es als Antwort gepostet. –

15
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; 
     } 
+0

Da diese Methode von der natürlichen Ausrichtung des Geräts abhängt, funktioniert sie nicht auf allen Geräten wie erwartet. –

2

landete ich über Zoltán Antwort mit auf, die großen Werke, außer wenn ich versuchte es auf einer Tablette (a Samsung P6210 Galaxy Tab 7.0 Plus). Im Hochformatmodus wurde SCREEN_ORIENTATION_REVERSE_PORTRAIT zurückgegeben. In der else-Anweisung (wenn die natürliche Ausrichtung eine Landschaft ist) habe ich die Fälle für ROTATION_90 und ROTATION_270 getauscht, und alles scheint gut zu funktionieren. (Ich habe nicht genug Ruf dies als Kommentar posten zu Zoltán Antwort.)

+0

Wissen Sie, ob dies auch auf anderen Landschaftstabellen passiert? Vielleicht sollten diese beiden Fälle für alle Naturlandschaftsgeräte getauscht werden? –

+0

Bestätigt, es passiert auch auf einem Asus Transformer Prime, tauschen die Fälle behoben. – Joan

16

Einfacher Ansatz wäre die Verwendung

getResources().getConfiguration().orientation 

1 für Potrait ist und 2 für die Landschaft.

+4

Die Frage erfordert detailliertere Informationen, d. H. Einschließlich umgekehrtes Hoch- und Querformat, die diese Funktionen nicht bereitstellen. –

1

Sie könnten es auf sehr einfache Weise tun: erhalten Sie den Bildschirm widht und height. Die Bildschirmbreite ist immer höher, wenn das Gerät im Querformat ausgerichtet ist.

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(); 
    } 
+1

Die Frage ist wirklich, wie man die detaillierte Orientierung erhält, d. H. Einschließlich, wie das Gerät gedreht wird. Die Methode, die Sie vorschlagen, unterscheidet nicht zwischen entgegengesetzten Orientierungen, z. Porträt und auf dem Kopf stehendes Portrait, etc. –

Verwandte Themen