在android中修改方向时查找屏幕角度

时间:2014-07-08 18:00:43

标签: android android-activity

我已在使用

的活动中修正了我的屏幕方向
     android:screenOrientation="portrait"

但是,我想找到用户当前持有屏幕的角度,即0,90或270.因此我必须执行一些操作。

 getWindowManager().getDefaultDisplay()
        .getRotation()

由于我已经修复了屏幕的方向,因此上面的代码将始终返回0。有人可以建议在这种情况下如何确定屏幕方向。

1 个答案:

答案 0 :(得分:0)

您的活动应实施SensorEventListeneronSensorChanged()方法:

protected void onCreate(Bundle savedInstanceState) {
    ...         
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    ...
}

@Override
public void onSensorChanged(SensorEvent event) {

    int ORIENTATION_UNKNOWN = -1;
    int _DATA_X = 0;
    int _DATA_Y = 1;
    int _DATA_Z = 2;

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        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;
        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
            orientation = compensateOrientation(orientation);
            while (orientation >= 360) {
                orientation -= 360;
            } 
            while (orientation < 0) {
                orientation += 360;
            }
        }

        if (orientation != lastOrientation) {
            lastOrientation = orientation;
            int generalOrientation = getGeneralOrientation(orientation);
            if(generalOrientation != GENERAL_ORIENTATION_UNCHANGED && generalOrientation != lastGeneralOrientation){ 
                lastGeneralOrientation = generalOrientation;
                onOrientationChanged(generalOrientation);
            }
        }
    }
}

/**
 * Only returns 4 orientations and gives some buffer zones where the orientation is not affected
 * 
 * @param degrees
 * @return
 */
private static int getGeneralOrientation(int degrees){
    if(degrees >= 330 || degrees <= 30 ) return 0;
    if(degrees <= 300 && degrees >= 240) return 270;
    if(degrees <= 210 && degrees >= 160) return 180;
    if(degrees <= 120 && degrees >= 60) return 90;
    return GENERAL_ORIENTATION_UNCHANGED;
}

您还可以使用此方法来补偿设备特定的方向。有些设备将肖像报告为0度,而其他设备可将其报告为270度。这需要处理所有组合:

/**
 * 
 * Compensate orientation based on the default rotation degrees for the device.
 * Some devices 0 degrees is landscape while on other 0 degrees is portrait
 * 
 * @param degrees
 * @return
 */
private int compensateOrientation(int degrees){
    Display display = getWindowManager().getDefaultDisplay();
    switch(display.getRotation()){
    case(Surface.ROTATION_270):
        return degrees + 270;
    case(Surface.ROTATION_180):
        return degrees + 180;
    case(Surface.ROTATION_90):
        return degrees + 90;
    default:
        return degrees;
    }
}

同时注册传感器监听器并取消注册,如下所示:

@Override
protected void onPause() {
    super.onPause();
    if(sensorManager != null) sensorManager.unregisterListener(this);
}

@Override
protected void onResume(){
    super.onResume();

    if(sensorManager != null) sensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);

}