5

Descrizioneorientamento dello schermo Android differisce tra dispositivi

Ho un'attività che visualizza l'anteprima fotocamera su una superficie vista. Non desidero che venga riavviato in caso di modifiche all'orientamento (poiché esegue costantemente la scansione dei codici QR), pertanto l'orientamento è bloccato in verticale.

Il problema che ho è che ho bisogno di visualizzare le istruzioni sullo schermo per l'utente nella parte inferiore dello schermo. Dato che lo schermo è bloccato in un orientamento, non mi viene notificato il nuovo orientamento poiché l'attività non viene ridisegnata. In questo modo le istruzioni sono state visualizzate nello stesso punto indipendentemente dal telefono ritratto, ritratto invertito, paesaggio o paesaggio riverito.

Per risolvere questo problema, ho collegato il gestore dei sensori per leggere l'orientamento in tempo reale. (Vedi il codice sotto) Dopo averlo letto in I lavoro fuori l'orientamento me stesso e spostare le mie istruzioni come richiesto come ogni aggiornamento mi è stato inviato. Questo ha funzionato per me fino a poco tempo fa. Durante il test su un tablet Asus Transformer Prime e Xoom. Il tablet restituisce l'orientamento come "90" per il ritratto, invece di "0" come i miei altri 2 telefoni. Quindi, come puoi vedere nel codice qui sotto, controllo i tablet (schermi grandi) e meno i 90 gradi in più.

Problema

Il problema è il nuovo tablet Nexus 7 non ha questo pregiudizio supplementare. In portrait restituisce "0", ma lo rilevo come un tablet, quindi meno 90 gradi quindi il risultato è 270 e posiziona le mie istruzioni come se il tablet fosse in modalità orizzontale. Presumo che il ragionamento alla base di questo è che Xoom/Transformer è stato progettato per essere utilizzato in orizzontale, e il Nexus in verticale, ma a meno che non conosca tutti i dispositivi interessati questo non aiuta.

Domanda

C'è un modo affidabile per rilevare l'orientamento di tutti i dispositivi e restituire i risultati in "tempo reale", come nel seguente codice, ma tenendo accoutn l'orientamento predefinito del dispositivo (ad esempio I telefoni + Nexus 7 sono verticali, ma la maggior parte dei tablet è orizzontale)?

codice attuale

boolean large = ((getBaseContext().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE); 
    boolean xlarge = ((getBaseContext().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);  
    isTablet = large || xlarge; 
    myOrientationEventListener = new OrientationEventListener(getBaseContext(), SensorManager.SENSOR_DELAY_NORMAL) 
    { 

     @Override 
     public void onOrientationChanged(int orientation) 
     { 
      if (orientation == -1) 
      { 
       return; 
      } 

      if (isTablet) 
      { 
       orientation += -90; 
       if (orientation < 0) // keep the result between 0-360 
       { 
        orientation += 360; 
       } 
      } 

      // Work out the orientation 

      if (orientation >= 60 && orientation <= 140) 
      { 
       screenOrientation = ScreenOrientation.Landscape_Reversed; 
      } 
      else if (orientation >= 140 && orientation <= 220) 
      { 
       screenOrientation = ScreenOrientation.Portrait_Reversed; 
      } 
      else if (orientation >= 220 && orientation <= 300) 
      { 
       screenOrientation = ScreenOrientation.Landscape; 
      } 
      else 
      { 
       screenOrientation = ScreenOrientation.Portrait; 
      } 

      //... Do stuff with new orientation here 
     } 
    }; 

risposta

5

fisso IT

sostituito il codice di

 if (isTablet) 
     { 
      orientation += -90; 
      if (orientation < 0) // keep the result between 0-360 
      { 
       orientation += 360; 
      } 
     } 

Con il seguente

  //Check "normal" screen orientation and adjust accordingly 
      int naturalOrientation = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); 
      if (naturalOrientation == Surface.ROTATION_0) 
      { 
       Logging.d(TAG, "Rotation ROTATION_0"); 
      } 
      else if (naturalOrientation == Surface.ROTATION_90) 
      { 
       Logging.d(TAG, "Rotation ROTATION_90"); 
       orientation += 90; 
      } 
      else if (naturalOrientation == Surface.ROTATION_180) 
      { 
       Logging.d(TAG, "Rotation ROTATION_180"); 
       orientation += 180; 
      } 
      else if (naturalOrientation == Surface.ROTATION_270) 
      { 
       Logging.d(TAG, "Rotation ROTATION_270"); 
       orientation += 270; 
      } 

      if (orientation > 360) // Check if we have gone too far forward with rotation adjustment, keep the result between 0-360 
      { 
       orientation -= 360; 
      } 
+1

[Questo articolo Developer] (http://android-developers.blogspot.com/2010/09/one-screen-turn-deserves-another.html) contiene alcune buone informazioni sull'argomento. – FoamyGuy

+0

Grazie a FoamyGuy. Non ho mai letto il blog, solo la guida per gli sviluppatori – JonWillis

Problemi correlati