设置请求的方向时检测方向更改

时间:2014-02-25 10:48:18

标签: android screen-orientation android-orientation

当屏幕方向改变时,是否可以获得事件

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

已设置?

这样做的目的是,一旦用户以全屏模式打开视频,屏幕就会出现风景。

当用户前进(后退)为肖像时,视频最小化。

2 个答案:

答案 0 :(得分:3)

这种活动方法将被调用。

 @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();
        }
    }

答案 1 :(得分:1)

试试这个:

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);
                }