且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找屏幕角度时,方向是固定在机器人

更新时间:2023-11-19 10:02:46

您的活动应该贯彻 SensorEventListener onSensorChanged()方法:

Your activity should implement the SensorEventListener and the onSensorChanged() method:

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度。这需要所有组合的护理:

You can also use this method to compensate for the device-specific orientation. Some devices report portrait as 0 degress, while other may report it as 270 degrees. This takes care of all the combinations:

/**
 * 
 * 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;
    }
}

另外注册传感器监听器和注销它是这样的:

Also register the sensor listener and unregister it like this:

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

}