Android Force Get screen orientation

Source: Internet
Author: User

Recently in the camera function, simple packaging system cameras, encountered a problem is that unable to get the current orientation of the screen caused the screen to rotate after the correct direction, so the picture is not in the right direction.

Check it out on the Internet. The current screen orientation is obtained through the WindowManager display or the Getorientation method of the configuration. But the problem is that the two methods require the activity not to be in a fixed direction and require the system to turn on automatic rotation to get the correct value, otherwise a fixed value will be returned.

Search to search is only these two answers, I think I can only use the monitoring direction sensor to achieve, but Android orientation sensor on the screen direction is almost unsupported. Finally in the API document Orientationeventlistener this thing, look at the document and think this thing is too scientific, more than using windowmanager/configuration/orientationsensor science , but the online can not find the introduction of his article, it is a pity!

To implement Orientationeventlistener, you need to create a member variable:

Orientationeventlistener Mscreenorientationeventlistener;
To initialize him:

this.mScreenOrientatio Neventlistener = new Orientationeventlistener (mactivity) {@Override public void Onorientationchange                d (int i) {//I's range is 0~359///screen left at the top of the time I = 90;                The top of the screen at the bottom of the time i = 180;                Screen right at the bottom of the time I = 270;                Normal condition default i = 0;                if (<= i && i < 135) {mscreenexiforientation = exifinterface.orientation_rotate_180; } else if (135 <= i && i < 225) {mscreenexiforientation = Exifinterface.ori                entation_rotate_270; } else if (225 <= i && i < 315) {mscreenexiforientation = Exifinterface.orientation_norma                L                } else {mscreenexiforientation = exifinterface.orientation_rotate_90; }            }        };

This way, regardless of whether the screen has a fixed orientation, regardless of whether the system is set to allow automatic rotation of the screen can get to the current direction of the screen!

When you turn on the monitor call listener.enable (), do not need to listen to the time listener.disable () just fine.

Android Force Get screen orientation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.