锁定手机方向时如何获取手机方向?

11

这可能是另一个问题的重复,我只是在努力找出要搜索什么。

我的相机应用程序被锁定在横屏模式(在清单中),就像这样:

android:screenOrientation="landscape"

然而,我希望在设备旋转为竖屏时仍然旋转一些UI元素(尽管安卓仍会认为它处于横屏模式,但这是有意的)。

因此,我已经尝试过以下代码来检查设备方向:

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认为方向是什么?


你是在什么时候检查方向的?在启动活动时吗? - Praful Bhatnagar
我希望能够不断检查它,以便可以立即对变化做出响应。因此,我现在对每个摄像头帧进行检查(至少暂时是这样)。 - Jameo
1
看一下这个:https://dev59.com/2H7aa4cB1Zd3GeqPvNAK#41104983 这个解决方案使用SensorManager。 - Maher Abuthraa
5个回答

13
所以在我思考了一下之后,我意识到我可以实现一个类似于Android本身用来确定方向的算法。
我使用onSensorChanged回调函数来完成这个任务。
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;
        
    }
}

看起来比实际复杂,但只要知道它是有效的(我会说和系统自带的一样好)。 别忘了在onResumeonPause中注册你的传感器变化事件监听器,用于加速度计。

仅在活动内有用。有许多情况需要在活动外确定方向。 - Johann
例如???另外,在问题中我承认我所做的不是正常的,但这是我使用的相机库所要求的。 - Jameo
处理图像的服务。 - Johann
这对于所要求的用例非常有用,干得好Jameo,谢谢。 - Prateek
运行得很好。看起来准确。 - vovahost

5

我使用以下代码向sensormanager注册以检测方向:

mSensorOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);      
mSensorManager.registerListener(this, mSensorOrientation, SensorManager.SENSOR_DELAY_NORMAL);

然后,这是用于检测方向变化的代码,您可以在注释中放置自己的方法实现。

常量:

public static final int LYING = 0;
public static final int LANDSCAPE_RIGHT = 1;
public static final int PORTRAIT = 2;
public static final int LANDSCAPE_LEFT = 3;



public void onSensorChanged(SensorEvent event) {

Sensor sensorEvent = event.sensor;

if ((sensorEvent.getType() == Sensor.TYPE_ORIENTATION)) {

    float [] eventValues = event.values;

    // current orientation of the phone
    float xAxis = eventValues[1];
    float yAxis = eventValues[2];

    if ((yAxis <= 25) && (yAxis >= -25) && (xAxis >= -160)) {

        if (previousOrientation != PORTRAIT){
            previousOrientation = PORTRAIT;

            // CHANGED TO PORTRAIT
        }

    } else if ((yAxis < -25) && (xAxis >= -20)) {

        if (previousOrientation != LANDSCAPE_RIGHT){
            previousOrientation = LANDSCAPE_RIGHT;

            // CHANGED TO LANDSCAPE RIGHT
        }

    } else if ((yAxis > 25) && (xAxis >= -20)){

        if (previousOrientation != LANDSCAPE_LEFT){
            previousOrientation = LANDSCAPE_LEFT;

            // CHANGED TO LANDSCAPE LEFT
        }
    }
}

}


1
Sensor.TYPE_ORIENTATION 在 API 8 中已被弃用。我现在正在测试,但可能需要找到更加向前兼容的东西。它声称 SensorManager.getOrientation 是应该使用的替代方法。 - Jameo
我找到了另一种解决方法,如果你还在使用原来的方法,你可以看看这个。它在我的Android 4.2手机上没有起作用。 - Jameo

3

在做了一些研究并尝试了一些东西后,当我将传感器设置为以下内容时,它对我有效:

mSensorOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

Sensor.TYPE_ORIENTATION已被废弃,根据一些不同人的示例代码来检索方向会给我带来错误的结果。我不知道它是否可行,但对我而言它是有效的。


2

这是@Jameo答案的稍微修改过的Kotlin版本。我需要角度来计算锁定方向的Activity中相机的旋转。请也给他点赞。

private var rotationDeg: Int = 0
private var rotationRoundedClockwise: Int = 0

override fun onSensorChanged(event: SensorEvent) {
    Timber.d("Sensor Changed")
    val newRotationDeg = calculateNewRotationDegree(event)
    //^^ thanks to google for that code
    // now we must figure out which orientation based on the degrees
    Timber.d("rotation: $newRotationDeg")
    if (newRotationDeg != rotationDeg) {
        rotationDeg = newRotationDeg
        rotationRoundedClockwise = calculateRoundedRotation(newRotationDeg)
    }
    Timber.d("rotationRoundedClockwise: $rotationRoundedClockwise")
}

private val X_AXIS_INDEX = 0
private val Y_AXIS_INDEX = 1
private val Z_AXIS_AXIS = 2
private val ORIENTATION_UNKNOWN = -1

private fun calculateRoundedRotation(newRotationDeg: Int): Int {
    return if (newRotationDeg <= 45 || newRotationDeg > 315) { // round to 0
        0  // portrait
    } else if (newRotationDeg in 46..135) { // round to 90
        90  // clockwise landscape
    } else if (newRotationDeg in 136..225) { // round to 180
        180  // upside down portrait
    } else if (newRotationDeg in 226..315) { // round to 270
        270  // anticlockwise landscape
    } else {
        0
    }
}

private fun calculateNewRotationDegree(event: SensorEvent): Int {
    val values = event.values
    var newRotationDeg = ORIENTATION_UNKNOWN
    val X = -values[X_AXIS_INDEX]
    val Y = -values[Y_AXIS_INDEX]
    val Z = -values[Z_AXIS_AXIS]
    val 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) {
        val ONE_EIGHTY_OVER_PI = 57.29577957855f
        val angle = Math.atan2((-Y).toDouble(), X.toDouble()).toFloat() * ONE_EIGHTY_OVER_PI
        newRotationDeg = 90 - Math.round(angle)
        // normalize to 0 - 359 range
        while (newRotationDeg >= 360) {
            newRotationDeg -= 360
        }
        while (newRotationDeg < 0) {
            newRotationDeg += 360
        }
    }
    return newRotationDeg
}

private fun getCameraRotation(): Int {
    return when (rotationRoundedClockwise) {
        0 -> 90
        90 -> 180
        180 -> 270
        270 -> 0
        else -> 90
    }
}

以下是如何监听事件的方法。

override fun onCreate() {
    super.onCreate()
    (activity?.getSystemService(SENSOR_SERVICE) as? SensorManager)?.let {
            sensorManager = it
        }
}

override fun onResume() {
    super.onResume()

    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL)
}

override fun onPause() {
    super.onPause()

    sensorManager.unregisterListener(this)
}

1

使用this作为参考,将@panavtec的答案翻译成API 23。

class MyActivity extends Activity implements SensorEventListener {

    private SensorManager sensorManager;
    private float[] lastMagFields = new float[3];;
    private float[] lastAccels = new float[3];;
    private float[] rotationMatrix = new float[16];
    private float[] orientation = new float[4];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    }

    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

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

    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                System.arraycopy(event.values, 0, lastAccels, 0, 3);
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                System.arraycopy(event.values, 0, lastMagFields, 0, 3);
                break;
            default:
                return;
        }

        if (SensorManager.getRotationMatrix(rotationMatrix, null, lastAccels, lastMagFields)) {
            SensorManager.getOrientation(rotationMatrix, orientation);

            float xAxis = (float) Math.toDegrees(orientation[1]);
            float yAxis = (float) Math.toDegrees(orientation[2]);

            int orientation = Configuration.ORIENTATION_UNDEFINED;
            if ((yAxis <= 25) && (yAxis >= -25) && (xAxis >= -160)) {
                Log.d(TAG, "Portrait");
                orientation = Configuration.ORIENTATION_PORTRAIT;
            } else if ((yAxis < -25) && (xAxis >= -20)) {
                Log.d(TAG, "Landscape Right");
                orientation = Configuration.ORIENTATION_LANDSCAPE;
            } else if ((yAxis > 25) && (xAxis >= -20)){
                orientation = Configuration.ORIENTATION_LANDSCAPE;
                Log.d(TAG, "Landscape Left");
            }
        }
    }
}

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