OrientationEventListener.onOrientationChangedによって報告された度とWindowManager().getDefaultDisplay().getRotationによって報告された表示回転との間の関係は何ですか?OrientationEventListenerとDisplay.getRotation
これらの両方は、デバイスの向きを度単位で返します。これらの価値観とはどのような関係があり、どのように解釈すればよいでしょうか?
OrientationEventListener.onOrientationChangedによって報告された度とWindowManager().getDefaultDisplay().getRotationによって報告された表示回転との間の関係は何ですか?OrientationEventListenerとDisplay.getRotation
これらの両方は、デバイスの向きを度単位で返します。これらの価値観とはどのような関係があり、どのように解釈すればよいでしょうか?
私はちょっと掘り下げて、アクティビティ内でネストされたクラスになる可能性のあるヘルパークラスを以下のように書きました。 OrientationEventListenerはchanges in the Accelerometer dataをリッスンします。電話機を自然な向き(縦向き/縦向き)にすると、向きは0になります。電話機を時計回りに回すと、電話機は徐々に大きくなります。したがって、左側が上にあるとき(風景)、向きは90です。電話が逆さまになっているときは180です。右側が上になると、270です。
getRotation画面の「自然な」方向からのものです。ドキュメントごとに、the angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device
。したがって、電話機がポートレートモード/自然の向きで保持されている場合、回転と向きは両方とも0になります。左側が上にある場合、向きは90ですが、描画されるグラフィックスはデバイスの回転。同様に、上辺の右辺が270、回転角度が90の場合も同様です。興味深いケース:電話機が上下逆さまになります。向きは180ですが、回転はまだ0です。電話機Display.getRotation()は変更されません。これは以下のコードからも確認できます。そこに変更され、向きが0の4つの値、90、180、に量子化されたとき、私は唯一の回転や向きを印刷しています270
static final class CustomOrientationListener extends OrientationEventListener {
private static final String TAG = CustomOrientationListener.class.getSimpleName();
private int savedOrientation = Integer.MIN_VALUE;
private int savedRotation = Integer.MIN_VALUE;
AppCompatActivity activity;
public CustomOrientationListener(AppCompatActivity a) {
super(a);
activity = a;
}
/**
* Called when the orientation of the device has changed.
* orientation parameter is in degrees, ranging from 0 to 359.
* orientation is 0 degrees when the device is oriented in its natural position,
* 90 degrees when its left side is at the top, 180 degrees when it is upside down,
* and 270 degrees when its right side is to the top.
*/
@Override
public void onOrientationChanged(int orientation) {
orientation = normalizeOrientation(orientation);
if (savedOrientation != orientation) {
Log.i(TAG, "orientation: " + orientation);
savedOrientation = orientation;
}
int rotation = getRotationInDegrees(activity.getWindowManager().getDefaultDisplay().getRotation());
if (rotation != savedRotation) {
Log.i(TAG, "rotation: " + rotation);
savedRotation = rotation;
}
}
/**
* getRotation() Returns the rotation of the screen from its "natural" orientation. The returned value may be
* Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270.
* For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a
* landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270
* depending on the direction it was turned. The angle is the rotation of the drawn graphics on the screen,
* which is the opposite direction of the physical rotation of the device. For example, if the device is rotated
* 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the
* returned value here will be Surface.ROTATION_90.
*/
private static int getRotationInDegrees(int rotation) {
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
return degrees;
}
private static int normalizeOrientation(int degrees) {
if (degrees > 315 || degrees <= 45) {
return 0;
}
if (degrees > 45 && degrees <= 135) {
return 90;
}
if (degrees > 135 && degrees <= 225) {
return 180;
}
if (degrees > 225 && degrees <= 315) {
return 270;
}
throw new RuntimeException("The physics as we know them are no more. Watch out for anomalies.");
}
}
偉大な自己の解答 - ただの質問に詳細を追加する - それはです閉じていない。リンクを追加するだけではなく、関連する詳細を追加してください –
ありがとうございます:) – exifguy