首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓版的UIDeviceOrientation和UIInterfaceOrientation是什么?

安卓版的UIDeviceOrientation和UIInterfaceOrientation是什么?
EN

Stack Overflow用户
提问于 2016-10-27 02:37:47
回答 1查看 132关注 0票数 1

在iOS上,设备方向可以与接口方向不同,因此我们有两个不同的枚举。我不一定要查找枚举。Android也有同样的区别吗?我想要检测接口方向的变化,而不是设备。

人们似乎在使用onConfigurationChanged()来检测方向的变化,但我并不清楚这到底是一种什么样的方向变化。

EN

回答 1

Stack Overflow用户

发布于 2016-10-27 02:47:55

这些方法可以检测(和)设置方向:

getResources().getConfiguration().orientationsetRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

我正在使用这些方法来捕获所有方向:

onCreate内幕

initOrientation();

哪里

代码语言:javascript
复制
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();
           }
    }

这取决于你真正想要实现的东西。上述方法可以检测portrait>portrait和landscape>landscape移位。

为了强制锁定和释放旋转,我使用这种方法:

代码语言:javascript
复制
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) -锁定

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40269810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档