android - 锁定到一个方向时获取手机方向

标签 android android-camera

这很容易与另一个问题重复,我只是在努力找出要搜索的内容。

我的相机应用程序被锁定在横向模式(在 list 中),如下所示:

android:screenOrientation="landscape"

但是,当设备旋转为纵向时,我仍想旋转一些 UI 元素(尽管 android 仍会认为它是横向的,但那是故意的)。

所以我试过这个来检查方向

int rotation = this.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0:
            Log.d("Rotation", "0");
            break;
        case Surface.ROTATION_90:
            Log.d("Rotation", "90");
            break;
        case Surface.ROTATION_180:
            Log.d("Rotation", "180");
            break;
        case Surface.ROTATION_270:
            Log.d("Rotation", "270");
            break;
    }

不幸的是,无论我如何转动手机,它总是返回 90。是否有更可靠的方法来获取方向,而不管 Android“认为”方向是什么?

最佳答案

所以在我考虑之后,我意识到我可以实现一个类似于 Android 本身用来确定方向的算法。我使用 onSenseorChanged 回调来完成此操作

public static final int UPSIDE_DOWN = 3;
public static final int LANDSCAPE_RIGHT = 4;
public static final int PORTRAIT = 1;
public static final int LANDSCAPE_LEFT = 2;
public int mOrientationDeg; //last rotation in degrees
public int mOrientationRounded; //last orientation int from above 
private static final int _DATA_X = 0;
private static final int _DATA_Y = 1;
private static final int _DATA_Z = 2;
private int ORIENTATION_UNKNOWN = -1;
@Override
public void onSensorChanged(SensorEvent event) 
{
    Log.d(TAG, "Sensor Changed");
    float[] values = event.values;
    int orientation = ORIENTATION_UNKNOWN;
    float X = -values[_DATA_X];
    float Y = -values[_DATA_Y];
    float Z = -values[_DATA_Z];        
    float magnitude = X*X + Y*Y;
    // Don't trust the angle if the magnitude is small compared to the y value
    if (magnitude * 4 >= Z*Z) {
        float OneEightyOverPi = 57.29577957855f;
        float angle = (float)Math.atan2(-Y, X) * OneEightyOverPi;
        orientation = 90 - (int)Math.round(angle);
        // normalize to 0 - 359 range
        while (orientation >= 360) {
            orientation -= 360;
        } 
        while (orientation < 0) {
            orientation += 360;
        }
    }
    //^^ thanks to google for that code
    //now we must figure out which orientation based on the degrees
    Log.d("Oreination", ""+orientation);
    if (orientation != mOrientationDeg) 
    {
        mOrientationDeg = orientation;
        //figure out actual orientation
        if(orientation == -1){//basically flat

        }
        else if(orientation <= 45 || orientation > 315){//round to 0
            tempOrientRounded = 1;//portrait
        }
        else if(orientation > 45 && orientation <= 135){//round to 90
            tempOrientRounded = 2; //lsleft
        }
        else if(orientation > 135 && orientation <= 225){//round to 180
            tempOrientRounded = 3; //upside down
        }
        else if(orientation > 225 && orientation <= 315){//round to 270
            tempOrientRounded = 4;//lsright
        }

    }

    if(mOrientationRounded != tempOrientRounded){
            //Orientation changed, handle the change here
        mOrientationRounded = tempOrientRounded;

    }
}

它看起来比实际更复杂,但只要知道它有效(我会说与系统一样有效)。不要忘记在加速度计的 onResume 和 onPause 中注册您的传感器更改事件监听器

关于android - 锁定到一个方向时获取手机方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14836003/

相关文章:

android - Grafika 和 OpenGL 在 android 上以方形录制视频

android - 无效的预览表面android视频录制

android - Droid 设备 - 不会旋转从相机捕获的图像? (相机.参数)

Android Camera参数setPictureSize导致图片出现条纹

android - Cordova 图片上传重复问题

Android - 套接字与轮询

android - 如何使用 android 在谷歌地图中显示替代路线?

javascript - 如何让 React Native 项目版本无缝跨项目和平台

androidx.fragment.app.Fragment 无法转换为 android.app.Fragment

android - 如何通过单击按钮添加和删除线性布局