如何更改方向但保持自动旋转屏幕

时间:2014-12-04 03:37:23

标签: android orientation

当你点击youtube app中的全屏图标时,它会变为横向和全屏,仍然会自动旋转模式,怎么做?

如果设置了lanscape模式:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)设备始终为横向

  1. 点击全屏
  2. Click fullscreen

    1. lanscape
    2. Lanscape

      1. 肖像
      2. portrain

        我解决了我的问题使用传感器检测风景或肖像

        @Override
        public void onPause() {
            super.onPause();
            videoview.pause();
            mSensorManager.unregisterListener(this);
        }
        
        @Override
        public void onResume() {
            super.onResume();
        
            mSensorManager = (SensorManager) getActivity().getSystemService(Activity.SENSOR_SERVICE);
            mRotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
            mSensorManager.registerListener(this, mRotationSensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
        
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor == mRotationSensor) {
                if (event.values.length > 4) {
                    float[] truncatedRotationVector = new float[4];
                    System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
                    update(truncatedRotationVector);
                } else {
                     update(event.values);
                }
            }
        
        }
        
        private void update(float[] vectors) {
            float[] rotationMatrix = new float[9];
            SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
            int worldAxisX = SensorManager.AXIS_X;
            int worldAxisZ = SensorManager.AXIS_Z;
            float[] adjustedRotationMatrix = new float[9];
            SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
            float[] orientation = new float[3];
            SensorManager.getOrientation(adjustedRotationMatrix, orientation);
            // float pitch = (float) Math.toDegrees(orientation[1]);
            float roll = (float) Math.toDegrees(orientation[2]);
        
            boolean curentOrient = isPortrait;
            if(roll >= -75 && roll <= 75){
                isPortrait = true;
                System.out.println("Portrait");
            }else{
                isPortrait = false;
                System.out.println("Landscape");
            }
            if(curentOrient != isPortrait){
                videoview.toggleFullScreen(isPortrait); // this is my videoview.
            }
        }
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
        

        实现SensorEventListener

3 个答案:

答案 0 :(得分:1)

使用ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE_SENSOR

答案 1 :(得分:1)

实际上,当您单击全屏按钮时,您可以仅以横向方向打开新活动。只是在那里播放视频。

答案 2 :(得分:0)

如果您只使用youtube视频,youtube API可以为您提供帮助。 据我所知,YoutubeActivity可以在您旋转设备时节省进度。这里链接:https://developers.google.com/youtube/android/player/?hl=ru