2014-12-23 11 views
8

Ich möchte die Kamerabildausrichtung im gesperrten Hochformatmodus finden, also benutze ich die Kamera in meiner Fragmentklasse und ich habe meine Bildschirmausrichtung bereits als Hochformat eingestellt, aber das Problem ist, wenn ich meine Kamera auswende Portrait to Landscape wird geändert und ich muss die Capture-Taste nur dann aktivieren, wenn sich die Kamera im Hochformat befindet. Kann mir jemand helfen, die Orientierungsänderungen im Hochformat zu bekommen? Unten ist mein Code:Wie erkenne ich meine Bildschirmausrichtung im Portrait gesperrt Bildschirm in Android?

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


    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     //do your stuff with the button 
    } 
} 

Wenn Sie Ihre Aktivitäten wollen nicht dann

android:configChanges="orientation|keyboardHidden|screenSize"

Wenn Sie während der Orientierung neu erstellt werden:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

sensorManager.registerListener(new SensorEventListener() { 

    int orientation=-1;; 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     if (event.values[1] < 6.5 && event.values[1] > -6.5) { 
      if (orientation!=1) { 
       Log.d("Sensor", "Landscape"); 
      } 
      orientation = 1; 
     } else { 
      if (orientation!=0) { 
       Log.d("Sensor", "Portrait"); 
      } 
      orientation = 0; 
     } 
    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    } 
}, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); 

if (orientation == 0) { 
    // capture button visisble 
} else { 
    // invisible 
} 
+0

http://stackoverflow.com/a/5726776/2247689 –

+0

einen Blick auf diese nehmen: http://stackoverflow.com/ a/41104983/2267723 diese Lösung mit SensorManager. –

Antwort

29

Sie können hierfür OrientationEventListener verwenden. Das ist Klasse, die es anpassen.

public abstract class SimpleOrientationListener extends OrientationEventListener { 

     public static final int CONFIGURATION_ORIENTATION_UNDEFINED = Configuration.ORIENTATION_UNDEFINED; 
     private volatile int defaultScreenOrientation = CONFIGURATION_ORIENTATION_UNDEFINED; 
     public int prevOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; 
     private Context ctx; 
     private ReentrantLock lock = new ReentrantLock(true); 

     public SimpleOrientationListener(Context context) { 
      super(context); 
      ctx = context; 
     } 

     public SimpleOrientationListener(Context context, int rate) { 
      super(context, rate); 
      ctx = context; 
     } 

     @Override 
     public void onOrientationChanged(final int orientation) { 
      int currentOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; 
      if (orientation >= 330 || orientation < 30) { 
       currentOrientation = Surface.ROTATION_0; 
      } else if (orientation >= 60 && orientation < 120) { 
       currentOrientation = Surface.ROTATION_90; 
      } else if (orientation >= 150 && orientation < 210) { 
       currentOrientation = Surface.ROTATION_180; 
      } else if (orientation >= 240 && orientation < 300) { 
       currentOrientation = Surface.ROTATION_270; 
      } 

      if (prevOrientation != currentOrientation && orientation != OrientationEventListener.ORIENTATION_UNKNOWN) { 
       prevOrientation = currentOrientation; 
       if (currentOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) 
        reportOrientationChanged(currentOrientation); 
      } 

     } 

     private void reportOrientationChanged(final int currentOrientation) { 

      int defaultOrientation = getDeviceDefaultOrientation(); 
      int orthogonalOrientation = defaultOrientation == Configuration.ORIENTATION_LANDSCAPE ? Configuration.ORIENTATION_PORTRAIT 
        : Configuration.ORIENTATION_LANDSCAPE; 

      int toReportOrientation; 

      if (currentOrientation == Surface.ROTATION_0 || currentOrientation == Surface.ROTATION_180) 
       toReportOrientation = defaultOrientation; 
      else 
       toReportOrientation = orthogonalOrientation; 

      onSimpleOrientationChanged(toReportOrientation); 
     } 

     /** 
     * Must determine what is default device orientation (some tablets can have default landscape). Must be initialized when device orientation is defined. 
     * 
     * @return value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} 
     */ 
     private int getDeviceDefaultOrientation() { 
      if (defaultScreenOrientation == CONFIGURATION_ORIENTATION_UNDEFINED) { 
       lock.lock(); 
       defaultScreenOrientation = initDeviceDefaultOrientation(ctx); 
       lock.unlock(); 
      } 
      return defaultScreenOrientation; 
     } 

     /** 
     * Provides device default orientation 
     * 
     * @return value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} 
     */ 
     private int initDeviceDefaultOrientation(Context context) { 

      WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
      Configuration config = context.getResources().getConfiguration(); 
      int rotation = windowManager.getDefaultDisplay().getRotation(); 

      boolean isLand = config.orientation == Configuration.ORIENTATION_LANDSCAPE; 
      boolean isDefaultAxis = rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180; 

      int result = CONFIGURATION_ORIENTATION_UNDEFINED; 
      if ((isDefaultAxis && isLand) || (!isDefaultAxis && !isLand)) { 
       result = Configuration.ORIENTATION_LANDSCAPE; 
      } else { 
       result = Configuration.ORIENTATION_PORTRAIT; 
      } 
      return result; 
     } 

     /** 
     * Fires when orientation changes from landscape to portrait and vice versa. 
     * 
     * @param orientation value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} 
     */ 
     public abstract void onSimpleOrientationChanged(int orientation); 

    } 

Dann, wo Sie Orientierung ermitteln möchten rufen Sie einfach

SimpleOrientationListener mOrientationListener = new SimpleOrientationListener(
       context) { 

      @Override 
      public void onSimpleOrientationChanged(int orientation) { 
       if(orientation == Configuration.ORIENTATION_LANDSCAPE){ 

       }else if(orientation == Configuration.ORIENTATION_PORTRAIT){ 

       } 
      } 
     }; 
     mOrientationListener.enable(); 
+0

Was soll ich innerhalb onSimpleOrientatioChanged-Methode deklarieren? – Vicky

+0

onCreate() wird tun. Sie müssen Ihren Code in dieser Methode schreiben, was Sie mit Orientierungsänderung tun wollen –

+0

gut ich brauche, wenn der Bildschirm wechselt von Hochformat zu Querformat-Schaltfläche sollte unsichtbar und nur sichtbar sein, wenn der Bildschirm im Hochformat ist – Vicky

0

Du wirst verwenden möchte, dass deine Aktivität gezwungen wird, Portrait zu bleiben, dann musst du

verwenden

in Ihrer Manifest-Datei in der Aktivität, die Sie einfach ausprobieren möchten android:screenOrientation="portrait"

Hoffe es hilft !!!

+0

Ich bekomme nullpointer Ausnahme @if (newConfig.orientation == Konfiguration.ORIENTATION_LANDSCAPE) – Vicky

+0

Wenn Sie nichts tun, wenn seine Landschaft dann entfernen Sie die if-Anweisung ... –

+0

gut, es ist nicht die richtige one eventouch, wenn ich den Bildschirm drehen zur Landschaft wird es als Porträt erkannt. – Vicky