2016-10-31 19 views
1

私のAndroid搭載デバイスの画面がある風景から別の風景に回転するタイミングを知る必要があります(rotation_90〜rotation_270)。 私のAndroidサービスでは、デバイスの回転を認識するためにonConfigurationChanged(Configuration newConfig)を再実装しました。しかし、この方法は、デバイスがORIENTATION_PORTRAITからORIENTATION_LANDSCAPEにローテーションされ、ORIENTATION_LANDSCAPE(90度)から他のORIENTATION_LANDSCAPE(270度)にローテーションされていない場合にのみ呼び出されます。Androidのランドスケープ画面180°回転

この場合、どのようにして呼び出すことができますか?おかげさまで

答えて

1

あなたのアクティビティに対してOrientationEventListenerを有効にすることができます。

OrientationEventListener mOrientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { 

     @Override 
     public void onOrientationChanged(int orientation) { 
      Log.v(TAG, "Orientation changed to " + orientation); 

      if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) { 
       return; 
      } 

      int degrees = -1; 
      if (orientation < 45 || orientation > 315) { 
       Log.i(TAG, "Portrait"); 
      } else if (orientation < 135) { 
       degrees = 90; 
       Log.i(TAG, "Landscape"); // This can be reverse landscape 
      } else if (orientation < 225) { 
       degrees = 180; 
       Log.i(TAG, "Reverse Portrait"); 
      } else { 
       degrees = 270; 
       Log.i(TAG, "Reverse Landscape"); // This can be landscape 
      } 
     } 
    }; 

    if (mOrientationListener.canDetectOrientation() == true) { 
     Log.v(TAG, "Can detect orientation"); 
     mOrientationListener.enable(); 
    } else { 
     Log.v(TAG, "Cannot detect orientation"); 
     mOrientationListener.disable(); 
    } 
+0

私は解決策になる可能性があります。このリスナーは、オリエンテーションのデバイスが変更されたときに呼び出されるため、あまり消費することはありませんか?私のアプリケーションは永久にアクティブです。 –

+0

これはあなたが望むように、システムトリガーです。角度が変わるたびに呼び出されます。オリエンテーションが完全に90度変化してこのリスナがトリガされるまで待機しません。だからそれはたくさん呼ばれています。これは、それらの "if-else"コントロールのポイントです。 – FatihC

+0

あなたは正しいです。それはこの情報を得る唯一の方法だと思われます。ありがとう。 –

0

あなたはこのコードを使用して、int型のメンバ変数として、前のオリエンテーションを保存することができます:

int oldRotation = getWindowManager().getDefaultDisplay().getRotation(); 

してから、デバイスを別のランドスケープモードから回転したかどうかを確認します。

if(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) { 
    int newRotation = getWindowManager().getDefaultDisplay().getRotation(); 
    if(newRotation != oldRotation) { 
     // rotation from 90 to 270, or from 270 to 90 
    } 
    oldRotation = newRotation; 
} 
+1

はい、私はそれを行いますが、テストをトリガするシステムハンドラを探しています。または、回転状態を定期的にテストする必要があります。 –

関連する問題