如何以度数获取Android设备的物理方向。

6
我知道我需要使用OrientationListener类从设备获取角度。我想要获取介于-90°和90°之间的角度,但我不知道该怎么做。 左边的图片表示90度,中间的图片表示0度,右边的图片表示-90度。 代码:
class OrientationListener implements SensorEventListener
{

    @Override
    public void onSensorChanged(SensorEvent event)
    {
        angle = Math.round(event.values[2]);

        if (angle < 0)
        {
            angle = angle * -1;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {
    }
}

使用重力传感器。 - njzk2
https://dev59.com/E2435IYBdhLWcg3w9FLi#5112135 - ElDuderino
1
@ElDuderino:你有实现它的例子吗? - funk
也许这会对你有所帮助:https://dev59.com/_G855IYBdhLWcg3wvW_d - user3336766
3个回答

2
你可以使用这段代码来实现简单的0度、90度和180度旋转。
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

int rotation = display.getRotation();

Surface.ROTATION_0代表0度,Surface.ROTATION_90代表90度等。

如果您需要除了0度、90度等之外的角度,您还可以使用SensorEventListener接口:

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        float rawX = event.values[0];
    }
}

您需要使用以下代码来获取角度:

double k = 90/9.8;
double degrees = rawX * k; // this is a rough estimate of degrees, converted from gravity

这不是我要找的。 - funk
display.getRotation() 会返回你的应用程序旋转方向,而不是物理旋转方向。 - user924
如果我们需要物理学位...我们应该去哪里获得呢?@user924 - gumuruh

1

这是一个可工作的示例。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView tv = new TextView(this);
    setContentView(tv);

    Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    int rotation = display.getRotation();

    String rotString="";
    switch(rotation) {
    case Surface.ROTATION_0:
        rotString="portrait";
        break;
    case Surface.ROTATION_90:
        rotString="landscape left";
        break;
    case Surface.ROTATION_180:
        rotString="flipped portrait";
        break;
    case Surface.ROTATION_270:
        rotString="landscape right";
        break;
    }

    tv.setText(rotString);

}

我的意思是举个例子来说明我的情况,如果我像图片中那样转动设备,我该如何获取值(请参见我的问题)。 - funk
你试过这个例子吗? - ElDuderino
是的,但我想要在-90度和90度之间的角度值。 - funk
display.getRotation() 将返回您的应用程序旋转信息,而非手机物理旋转方向。 - user924

1

这是一个老问题,但我在尝试弄清楚相机的旋转角度(以度为单位)时发现了OrientationEventListener()。

 public void onOrientationChanged(int orientation) {
     if (orientation == ORIENTATION_UNKNOWN) return;
     android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     orientation = (orientation + 45) / 90 * 90;
     int rotation = 0;
     if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
         rotation = (info.orientation - orientation + 360) % 360;
     } else {  // back-facing camera
         rotation = (info.orientation + orientation) % 360;
     }
     mParameters.setRotation(rotation);
 }

这里的答案对我很有帮助,因为我没有像开发者指南中Handling Runtime Changes Yourself主题中讨论的那样捕获onConfigurationChanged()。 我只是要在MyActivity的onSurfaceCreated()中使用Elduderino在这里提出的display.getRotation()方法来适当设置相机的旋转。 这将起作用,因为每当设备方向更改时,默认情况下会重新创建表面。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接