【问题标题】:How to get the Android device's physical orientation in degrees.如何以度为单位获取 Android 设备的物理方向。
【发布时间】:2014-03-17 13:46:00
【问题描述】:

我知道我必须使用 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)
    {
    }
}

【问题讨论】:

标签: android


【解决方案1】:

您可以将此代码用于简单的 0、90、180 度。

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

int 旋转 = 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

【讨论】:

  • 这不是我要找的
【解决方案2】:

这是一个工作示例。

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);

}

【讨论】:

  • 我的意思是我的情况的一个例子,如果我像图片中那样转动设备来获得价值(见我的问题)
  • 你试过这个例子了吗?
  • 是的。但我想要 -90 到 90 度之间的度数
【解决方案3】:

这是一个老问题,但我在试图找出 rotation for a camera 的度数时发现了一个 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()

我将简单地使用Elduderino 在MyActivity 的onSurfaceCreated() 中提出的display.getRotation() 方法来适当地设置相机的旋转。当设备方向发生变化时,默认情况下会重新创建表面,这将起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多