gpt4 book ai didi

android - OrientationEventListener 与 Display.getRotation

转载 作者:行者123 更新时间:2023-11-29 19:09:46 24 4
gpt4 key购买 nike

OrientationEventListener.onOrientationChanged报告的学位之间有什么关系?和 WindowManager().getDefaultDisplay().getRotation 报告的显示旋转.

这两个都以度数返回设备的方向。这些值(value)观之间有什么关系,应该如何解读它们?

最佳答案

所以我做了一些挖掘并编写了一个辅助类,如下所示,它可以是 Activity 中的嵌套类。 OrientationEventListener 监听 changes in the Accelerometer data .如果您以自然方向(纵向/垂直)握持手机,则方向为 0。顺时针方向旋转手机时方向逐渐增加。所以当左侧在上面(横向)时,方向是 90。当手机倒置时,它是 180。当右侧向上时,它是 270。

虽然 getRotation() 是屏幕从其“自然”方向的旋转。根据文档,角度是屏幕上绘制图形的旋转,与设备的物理旋转方向相反。因此,当手机处于纵向模式/自然方向时,旋转和方向均为 0。如果左侧在顶部,则方向为 90,但旋转为 270,因为绘制的图形与方向相反设备旋转。同样,如果右侧在顶部,则方向为 270,旋转为 90。有趣的情况:手机倒置 - 方向为 180,但旋转仍然为 0。如果在电话时不重绘图形旋转,则 Display.getRotation() 保持不变。从下面的代码也可以验证这一点。我只在发生变化时打印旋转和方向,并且方向已量化为 0、90、180、270 的 4 个值。

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.");
}
}

关于android - OrientationEventListener 与 Display.getRotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45783793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com