开发者

Detecting the screen orientation in android

I am implementing a camera in android. I have kept the activity as landscape in t开发者_Python百科he manifest. Since i have given the orientation as fixed, i am not able to get orientation by display. it always gives as LandScape. But i want to know when my device is held in portrait or vertical position. I do not want the screen orientation. Can any one suggest a good way to detect device orientation.

Thanks all


I think you will have to listen to the accelerometer sensor updates and parse them to determine when the orientation changes. there is some examples of listening to the sensors here: http://www.anddev.org/accessing_the_accelerometer-t499.html and here http://mobilestrategist.blogspot.com/2010/01/android-accelerometer-and-orientation.html


    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

//          Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

//          Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
        // Checks whether a hardware keyboard is available
        if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
//          Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
        } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
//          Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
        }
    }


To detect screen orientation you can use the following code in your activity

@Override
    public void onConfigurationChanged(Configuration newConfig)
    { super.onConfigurationChanged(newConfig);
    }

Thanks Deepak


Try this: First implement SensorEventListener and get the RotationSensor

sensorManager = (SensorManager)getActivity().getSystemService(Context.SENSOR_SERVICE);
rotationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
sensorManager.registerListener(this, rotationSensor, SENSOR_INTERVAL);
int FROM_RADS_TO_DEGS = -57;

Then you can detect the angle of the device like this:

@Override
public void onSensorChanged(SensorEvent event) {
    if(event.sensor == rotationSensor) {
        if (event.values.length > 4) {
            float[] truncatedRotationVector = new float[4];
            System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
            updateRotation(truncatedRotationVector);
        } else {
            updateRotation(event.values);
        }
    }
}

private void updateRotation(float[] vectors) {
    float[] rotationMatrix = new float[9];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
    int worldAxisX = SensorManager.AXIS_X;
    int worldAxisZ = SensorManager.AXIS_Z;
    float[] adjustedRotationMatrix = new float[9];
    SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
    float[] orientation = new float[3];
    SensorManager.getOrientation(adjustedRotationMatrix, orientation);
    float pitch = orientation[1] * FROM_RADS_TO_DEGS;
    if(pitch < -45 && pitch > -135) {
        // if device is laid flat on a surface, we don't want to change the orientation
        return;
    }
    float roll = Math.abs(orientation[2] * FROM_RADS_TO_DEGS);
    if((roll > 45 && roll < 135)) {
        // The device is closer to landscape orientation. Enable fullscreen
        if(!player.isFullScreen()) {
            if(getActivity() != null) {
                player.setFullScreenOn();
            }
        }
    }
    else {
        // The device is closer to portrait orientation. Disable fullscreen
        if(player.isFullScreen()) {
            if(getActivity() != null) {
                player.setFullScreenOff();
            }
        }
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Do nothing
}

This got the original code from a tutorial, but it was a while ago, so I cant remember where the tutorial was. This version is heavily customised to my requirements, but if anyone recognises it from the original, please drop in the link.

I used this code to detect when a video player should go fullscreen inside a ViewPage that I didn't want to allow landscape orientation on. It works well except for one thing:

It uses RotationSensor hardware, and not all Android devices have a RotationSensor. If anyone knows of a way to do it using some hardware that is included on all devices (There definitely is a way because Android knows when to switch orientation), please let me know in a comment so I can update my own code.


I use this:

@Override
    public void onConfigurationChanged(Configuration _newConfig){
        super.onConfigurationChanged(_newConfig);
        int height = getWindowManager().getDefaultDisplay().getHeight();
        int width = getWindowManager().getDefaultDisplay().getWidth();
        if(width > height){
// landscape
        }else{
// portrait
        }
    }

It is crude but effective.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜