简体   繁体   中英

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. I'm not necessarily looking for enums. Does Android make the same distinction? 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.

this methods can detect (and) set orientation:

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

I'm using those methods to catch all orientations:

inside 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

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