vuhung3990
vuhung3990

Reputation: 6789

How to change orientation but keep auto rotage screen

when you click fullscreen icon in youtube app, it will change to landscape and fullscreen , devece still auto rotage mode, how to do that?

if set lanscape mode : setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) (device always landscape)

  1. click fullscreen

Click fullscreen

  1. lanscape

Lanscape

  1. portrait

portrain

I SOLVED MY PROBLEM using sensor to detect landscape or portrait

@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) {
}

and let implements SensorEventListener

Upvotes: 0

Views: 138

Answers (3)

DMan
DMan

Reputation: 268

In case when you will use only youtube video youtube API can help you. As i know YoutubeActivity can save progress when you rotate device. Here link: https://developers.google.com/youtube/android/player/?hl=ru

Upvotes: 0

QArea
QArea

Reputation: 4981

Actually when you click fullscreen button you can open new activity with only landscape orientation. Just play video there.

Upvotes: 1

Larry Schiefer
Larry Schiefer

Reputation: 15775

Use ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE_SENSOR.

Upvotes: 1

Related Questions