David.Ko

          Follow my heart!
          posts - 100, comments - 11, trackbacks - 0, articles - 0
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Lock Screen Orientation in Android

          Posted on 2010-06-21 23:46 David.Ko 閱讀(1618) 評(píng)論(0)  編輯  收藏 所屬分類: Android
          Martin Belcher 26th May 2010

          IntroAndroid screen rotation image

          This article describes how to force the orientation of an Android view not to change ie screen not to rotate.


          How to lock the orientation

          In the onCreateDialog(int) event of the activity use the setRequestedOrientation(int) method to set the screen orientation to your chosen setting. The activity will stay in this orientation regardless of if the device is tilted or not.

          [Code sample – How to lock the orientation]
          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState
          {
              super.onCreate(savedInstanceState);
              this.setRequestedOrientation(
          ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
          }

          How to detect the current orientation

          To programmatically detect the current orientation of the activity use the following code snippet. Theorientation property of the Configuration class returns three possible values corresponding to Landscape, Portrait and Square.

          [Code sample – How to detect the current orientation]
          switch (this.getResources().getConfiguration().orientation)
          {
          case Configuration.ORIENTATION_PORTRAIT:
            // Do something here
            break;
          case Configuration.ORIENTATION_LANDSCAPE:
            // Do something here
            break;
          case Configuration.ORIENTATION_SQUARE:
            // Do something here
            break;
          default:
            throw new Exception("Unexpected orientation enumeration returned");
            break;
          }

          Example : Locking rotation while performing an action.

          You might wish to disable the screen rotation whilst performing an action or by user command, to do this you need to combine the above samples to detect the current orientation and lock the display to that orientation.

          [Code sample – Locking rotation while performing an action]
          // Sets screen rotation as fixed to current rotation setting
          private void mLockScreenRotation()
          {
            // Stop the screen orientation changing during an event
              switch (this.getResources().getConfiguration().orientation)
              {
            case Configuration.ORIENTATION_PORTRAIT:
              this.setRequestedOrientation(
          ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
              break;
            case Configuration.ORIENTATION_LANDSCAPE:
              this.setRequestedOrientation(
          ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
              break;
              }
          }

          Once your action has completed you may wish to enable screen rotation again, see the next section for an example on how to do this.


          How to re-enable screen rotation

          To enable the orientation to be automatically changed on device tilt simply pass thesetRequestedOrientation(int) method the enumeration value for an unspecified orientation.

          [Code sample – How to re-enable screen rotation]
          // allow screen rotations again
          this.setRequestedOrientation(
          ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
          主站蜘蛛池模板: 惠州市| 湘乡市| 仙居县| 漳州市| 屏东县| 沂南县| 若羌县| 大埔县| 冷水江市| 双鸭山市| 仁怀市| 墨竹工卡县| 东方市| 静宁县| 礼泉县| 柳林县| 林甸县| 阿拉尔市| 依兰县| 策勒县| 贵德县| 浪卡子县| 河东区| 都江堰市| 遂溪县| 新昌县| 阿瓦提县| 衡东县| 兴隆县| 栾川县| 台中县| 蓬莱市| 什邡市| 行唐县| 金山区| 如东县| 崇义县| 余干县| 双峰县| 大冶市| 龙口市|