简体   繁体   English

什么是UIDeviceOrientation和UIInterfaceOrientation的Android等价物?

[英]What is the Android equivalent of UIDeviceOrientation and UIInterfaceOrientation?

On iOS, device orientation can be different from interface orientation, so we have two different enums. 在iOS上,设备方向可能与界面方向不同,因此我们有两个不同的枚举。 I'm not necessarily looking for enums. 我不一定要找枚举。 Does Android make the same distinction? Android有同样的区别吗? I want to detect interface orientation changes rather than device. 我想检测界面方向更改而不是设备。

People seem to be using onConfigurationChanged() to detect orientation changes, but it wasn't clear to me exactly what kind of orientation change this is. 人们似乎正在使用onConfigurationChanged()来检测方向变化,但我不清楚这究竟是什么样的方向变化。

this methods can detect (and) set orientation: 这种方法可以检测(和)设置方向:

getResources().getConfiguration().orientation and setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); getResources().getConfiguration().orientationsetRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

I'm using those methods to catch all orientations: 我正在使用这些方法来捕捉所有方向:

inside onCreate : onCreate里面:

initOrientation();

where 哪里

private void initOrientation() {
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        mLastRotation = mWindowManager.getDefaultDisplay().getRotation();
        myOrientationEventListener
        = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI){
         @Override
         public void onOrientationChanged(int orientation) {
//         Log.d(TAG, "ORIENTATION: " + orientation);
         Display display = mWindowManager.getDefaultDisplay();
         int rotation = display.getRotation();
         if (((rotation == Surface.ROTATION_90 && mLastRotation == Surface.ROTATION_270)
                 || (rotation == Surface.ROTATION_270 && mLastRotation == Surface.ROTATION_90)
                 || (rotation == Surface.ROTATION_0 && mLastRotation == Surface.ROTATION_180)
                 || (rotation == Surface.ROTATION_180 && mLastRotation == Surface.ROTATION_0))) {
             //if ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && rotation != mLastRotation) {
             Log.i(TAG, "changed >>> " + rotation + " :: " + _.mWidth);

             // do something

             mLastRotation = rotation;
         }
         }
         };

           if (myOrientationEventListener.canDetectOrientation()){
            Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
            myOrientationEventListener.enable();
           }
           else{
            Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
          //  finish();
           }
    }

it depends on what your really want to realize. 这取决于你真正想要实现的目标。 the method above can detect portrait>portrait and landscape>landscape shifts. 上面的方法可以检测肖像>纵向和横向>横向移动。

In order to force lock and release rotation I'm using this approach: 为了强制锁定和释放旋转,我使用这种方法:

protected void mLockScreenRotation(int i)
{
  // Stop the screen orientation changing during an event
    switch (i)
    {
    default:
    case 0:
        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;
        }
    case 1:
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        break;
    case 2:
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        break;
    }
}

mLockScreenRotation(0) - locks mLockScreenRotation(0) - 锁定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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