Android Device Orientation

Source: Internet
Author: User
Tags in degrees

Recently in the direction of the camera to deal with the problem, in the Android device orientation problems, there are some questions on the way to write a demo to understand how the Android device orientation is a judgment.

Android Device Orientation is the most common use of video playback software, it will be with you fiddling with the direction of the phone, to adjust the most suitable for the screen rotation for the user to watch. The official API document has the same word for Android Device orientation:

Public abstract void onorientationchanged (int orientation) Added toAPI Level 3

Called when the orientation of the device has changed. Orientation parameter is in degrees, ranging from 0 to 359. Orientation was 0 degrees when the device was oriented in its natural position, and the degrees when it left side was at the top, Degrees when it was upside down, and degrees when it was right side was to the top. was returned when the device was close to flat and the ORIENTATION_UNKNOWN orientation cannot was determined.

For this sentence, one thing does not understand is that natural position is a position, in fact, is our normal use of mobile phone placement direction, such as: orientation for 0

Left Side Top, orientation is 90:

Right Side Top, orientation is 270:

Top Side down,orientation for 180

Well, that's pretty much it. Put the code that I used to test again:

1  Public classMainactivityextendsactionbaractivity {2 3     4     PrivateTextView TextView =NULL;5     PrivateMyorientationlistener Myorientationlistener =NULL;6 @Override7     protected voidonCreate (Bundle savedinstancestate) {8         Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);TenTextView =(TextView) Findviewbyid (R.id.textview); OneMyorientationlistener =NewMyorientationlistener ( This, sensormanager.sensor_delay_normal); A         if(Myorientationlistener.candetectorientation ()) { - myorientationlistener.enable (); -         } the         Else{ -Toast.maketext ( This, "Can ' t Detect Orientation", Toast.length_long). Show (); -         } -      +     } -  + @Override A      Public BooleanOncreateoptionsmenu (Menu menu) { at         //inflate the menu; This adds items to the action bar if it is present. - getmenuinflater (). Inflate (R.menu.main, menu); -         return true; -     } -      - @Override in     protected voidOnDestroy () { -         Super. OnDestroy (); to myorientationlistener.disable (); +     } -      the @Override *     protected voidOnresume () { $         Super. Onresume ();Panax Notoginseng         if(Myorientationlistener.candetectorientation ()) { - myorientationlistener.enable (); the         } +         Else{ AToast.maketext ( This, "Can ' t Detect Orientation", Toast.length_long). Show (); the         } +     } -  $ @Override $      Public Booleanonoptionsitemselected (MenuItem item) { -         //Handle Action Bar item clicks here. The Action Bar would -         //automatically handle clicks on the Home/up button, so long the         //As you specify a the parent activity in Androidmanifest.xml. -         intID =Item.getitemid ();Wuyi         if(id = =r.id.action_settings) { the             return true; -         } Wu         return Super. onoptionsitemselected (item); -     } About      $      -     Private classMyorientationlistenerextendsorientationeventlistener{ -  -          PublicMyorientationlistener (Context context,intRate ) { A             Super(context, rate); +             //TODO auto-generated Constructor stub the         } -  $ @Override the          Public voidOnorientationchanged (intorientation) { the             //TODO auto-generated Method Stub theTextview.settext ("" +orientation); the         } -  in          the          the     } About}
View Code

 

Android Device 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.