简体   繁体   中英

Android screen - angle of rotation

Question is related to rotation of the android emulator screen.

I am using DefaultDisplay.getRotation() to get the rotation angle on the emulator. It returns 0 and 90 only. Even in reverse portrait and reverse landscape. Have not set any specific Screen orientation in the activity XML file. I am a beginner at this so am probably missing something here and could use some help understanding what that might be.

Thanks.

Try this..

int rotation = getWindowManager().getDefaultDisplay().getRotation();
        Log.v("rotation--", ""+rotation);

        switch (rotation) {

        case 0:
            Toast.makeText(getBaseContext(), "Angle 0", Toast.LENGTH_SHORT).show();
            break;
        case 1:
            Toast.makeText(getBaseContext(), "Angle 90", Toast.LENGTH_SHORT).show();
            break;
        case 2:
            Toast.makeText(getBaseContext(), "Angle 180", Toast.LENGTH_SHORT).show();
            break;
        case 3:
            Toast.makeText(getBaseContext(), "Angle 270", Toast.LENGTH_SHORT).show();
            break;
        }

First of all getRotation method doesn't work in emulator screen. Because according to your thread you can see you are getting only two angles ie "0" and "90" because of landscape and potrait mode . So just test it in any real device.

If you just want to portrait/reverse portrait and landscape/reverse landscape you can use

            if (Utils.getDeviceDefaultOrientation(localActivity) == Configuration.ORIENTATION_LANDSCAPE)
            {
                if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_0)
                    //Landscape Mode
                else if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_90)
                    //Portrait Mode
                else if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_180)
                    //Reverse Landscape
                else if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_270)
                    //Reverse Portrait
            }
            else
            {
                if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_0)
                    //Portrait Mode
                else if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_90)
                    //Landscape Mode
                else if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_180)
                    //Reverse Portrait Mode
                else if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_270)
                    //Reverse Landscape Mode
            }

We have to check for getDeviceDefaultOrientation because for tablets the default device orientation is Landscape and it will return getRotation() 0 in that case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM