【问题标题】:Detect orientation change while requested orientation is set在设置请求方向时检测方向变化
【发布时间】:2014-11-17 22:23:51
【问题描述】:

当屏幕方向改变时是否可以得到一个事件

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

设置好了吗?

这样做的目的是,一旦用户以全屏模式打开视频,屏幕就会变成横向。

当用户转到(返回)纵向时,视频会最小化。

【问题讨论】:

    标签: android screen-orientation android-orientation


    【解决方案1】:

    这个活动方法将被调用。

     @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
    
            // Checks the orientation of the screen
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
                Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
            }
        }
    

    【讨论】:

    • 嗯,一旦我设置了方向,我就不会再被调用了。
    • 我也有兴趣获得所有 4 个方向,但尚未找到合适的解决方案。我开始实施传感器。但这包括设备的所有方向。想知道,我怎么只能收到0,90,180,270。
    • @icbytes,看这个问题stackoverflow.com/questions/3611457/…
    • 请分享您的代码。我认为您在 oncreate 方法中使用它。
    • 好吧,我不知道您是否自己尝试过,但是一旦您设置了请求的方向(横向或纵向),将不再调用 onConfigurationChanged。谢谢
    【解决方案2】:

    试试这个:

    private OrientationEventListener mOrientationEventListener;
    private int mOrientation =  -1;
    private static final int ORIENTATION_PORTRAIT_NORMAL =  1;
    private static final int ORIENTATION_PORTRAIT_INVERTED =  2;
    private static final int ORIENTATION_LANDSCAPE_NORMAL =  3;
    private static final int ORIENTATION_LANDSCAPE_INVERTED =  4;
    
     @Override
     protected void onResume() {
                     super.onResume();
                     if (mOrientationEventListener == null) {
                         mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
    
                             @Override
                             public void onOrientationChanged(int orientation) {
    
                             // determine our orientation based on sensor response
                             int lastOrientation = mOrientation;
                             if (orientation >= 315 || orientation < 45) {
                                 if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
                                     mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                                     }
                                 }
                             else if (orientation < 315 && orientation >= 225) {
                                 if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
                                     mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                                     }
                                 }
                             else if (orientation < 225 && orientation >= 135) {
                                 if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                                     mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                                     }                                        }
                             else {
                                 // orientation <135 && orientation > 45
                                 if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                                     mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                                     }                                        }
                             if (lastOrientation != mOrientation) {
                            //   changeRotation(mOrientation, lastOrientation);
                                 }
                             }
                             };
                                 }
    
                     if (mOrientationEventListener.canDetectOrientation()) {
                         mOrientationEventListener.enable();
                         }
                     }
    
                 public void onOrientationChanged(int orientation) {
    
                     // determine our orientation based on sensor response
                     int lastOrientation = mOrientation;
                     Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
                     if (display.getOrientation() == Surface.ROTATION_0) {
                         // landscape oriented devices
                         if (orientation >= 315 || orientation < 45) {
                             if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
                                 mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                                 }         }
                         else if (orientation < 315 && orientation >= 225) {
                             if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                                 mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                                 }
                             } else if (orientation < 225 && orientation >= 135) {
                                 if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                                     mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                                     }
                                 } else if (orientation <135 && orientation > 45) {
                                     if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
                                         mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                                         }                                }
                         } else {
                             // portrait oriented devices
                             if (orientation >= 315 || orientation < 45) {
                                 if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
                                     mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                                     }
                                 } else if (orientation < 315 && orientation >= 225) {
                                     if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
                                         mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                                         }
                                     } else if (orientation < 225 && orientation >= 135) {
                                         if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                                             mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                                             }
                                         } else if (orientation <135 && orientation > 45) {
    
                                             if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                                                 mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                                                 }
                                             }     }
                     if (lastOrientation != mOrientation) {
    
                    //   changeRotation(mOrientation, lastOrientation);     } }
    
                     }
                 }
    
                 @Override
                        public void onConfigurationChanged(Configuration newConfig) {
                            super.onConfigurationChanged(newConfig);
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多