Android横竖屏手动以及自动切换的实现

之前项目做摄像头的采集视频播放,里面有关于横竖屏的显示内容,这里把实现的方法给大家展示出来。

首先创建横竖屏监听类,需要传入当前activity,同时提供了传入button来实现手动全屏和退出全屏的操作。

ScreenOrientationHelper监听类

public class ScreenOrientationHelper implements SensorEventListener {

private final static String TAG = ScreenOrientationHelper.class.getSimpleName();

private Activity mActivity;
private CheckTextButton mButton1, mButton2;
private int mOriginOrientation;
private Boolean mPortraitOrLandscape;

private SensorManager mSensorManager;
private Sensor[] mSensors;

private float[] mAccelerometerValues = new float[3];
private float[] mMagneticFieldValues = new float[3];

public ScreenOrientationHelper(Activity activity) {
    this(activity, null, null);
}

public ScreenOrientationHelper(Activity activity, CheckTextButton button) {
    this(activity, button, null);
}

public ScreenOrientationHelper(Activity activity, CheckTextButton button1, CheckTextButton button2) {
    mActivity = activity;
    mSensorManager = (SensorManager) mActivity.getSystemService(Context.SENSOR_SERVICE);

    mButton1 = button1;竖屏全屏按钮
    if (mButton1 != null) {
        mButton1.setEnabled(false);
        mButton1.setToggleEnable(false);
        mButton1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSensors != null) {
                    if (mButton1.isChecked())
                        portrait();//竖屏
                    else
                        landscape();//横屏
                }
            }
        });
    }

    mButton2 = button2;//横屏全屏按钮
    if (mButton2 != null) {
        mButton2.setToggleEnable(false);
        mButton2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSensors != null) {
                    if (mButton2.isChecked())
                        portrait();//竖屏
                    else
                        landscape();//横屏
                }
            }
        });
    }
}

public void enableSensorOrientation() {
    if (mSensors == null) {
        mOriginOrientation = mActivity.getRequestedOrientation();

        mSensors = new Sensor[2];
        mSensors[0] = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensors[1] = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        mSensorManager.registerListener(this, mSensors[0], SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, mSensors[1], SensorManager.SENSOR_DELAY_NORMAL);
    }
    mButton1.setEnabled(true);
}

public void disableSensorOrientation(boolean reset) {
    if (mSensors != null) {
        mSensorManager.unregisterListener(this, mSensors[0]);
        mSensorManager.unregisterListener(this, mSensors[1]);
        mSensors = null;

        if (reset == true) {
            mActivity.setRequestedOrientation(mOriginOrientation);
        }
    }
    mButton1.setEnabled(false);
}

public void disableSensorOrientation() {
    disableSensorOrientation(true);
}

public void landscape() {
    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setButtonChecked(true);
    mPortraitOrLandscape = false;
}

public void portrait() {
    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setButtonChecked(false);
    mPortraitOrLandscape = true;
}

public void setButtonChecked(boolean checked) {
    if (mButton1 != null)
        mButton1.setChecked(checked);

    if (mButton2 != null)
        mButton2.setChecked(checked);
}

public void postOnStart() {
    if (mSensors != null) {
        mSensorManager.registerListener(this, mSensors[0], SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, mSensors[1], SensorManager.SENSOR_DELAY_NORMAL);
    }
}

public void postOnStop() {
    if (mSensors != null) {
        mSensorManager.unregisterListener(this, mSensors[0]);
        mSensorManager.unregisterListener(this, mSensors[1]);
    }
}

private void calculateOrientation() {
    float[] values = new float[3];
    float[] R = new float[9];

    SensorManager.getRotationMatrix(R, null, mAccelerometerValues, mMagneticFieldValues);
    SensorManager.getOrientation(R, values);

    if (mSensors != null) {
        if (mSensors[1] == null)
            calculateByAccelerometer(mAccelerometerValues);
        else
            calculateByOrientation(values);
    }
}

private void calculateByAccelerometer(float[] values) {
    int orientation = mActivity.getRequestedOrientation();

    if ((-2f < values[1] && values[1] <= 2f) && values[0] < 0) {// 向左
        if (orientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                && (mPortraitOrLandscape == null || !mPortraitOrLandscape)) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setButtonChecked(true);
        }

        if (mPortraitOrLandscape != null && !mPortraitOrLandscape)
            mPortraitOrLandscape = null;

    } else if (4f < values[1] && values[1] < 10f) { // 向下
        if (orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                && (mPortraitOrLandscape == null || mPortraitOrLandscape)) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            setButtonChecked(false);
        }

        if (mPortraitOrLandscape != null && mPortraitOrLandscape)
            mPortraitOrLandscape = null;

    } else if (-10f < values[1] && values[1] < -4f) { // 向上
        if (orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                && (mPortraitOrLandscape == null || mPortraitOrLandscape)) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setButtonChecked(false);
        }

        if (mPortraitOrLandscape != null && mPortraitOrLandscape)
            mPortraitOrLandscape = null;

    } else if ((-2f < values[1] && values[1] <= 2f) && values[0] > 0) { // 向右
        if (orientation != ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
                && (mPortraitOrLandscape == null || !mPortraitOrLandscape)) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            setButtonChecked(true);
        }

        if (mPortraitOrLandscape != null && !mPortraitOrLandscape)
            mPortraitOrLandscape = null;
    }
}

private void calculateByOrientation(float[] values) {
    values[0] = (float) Math.toDegrees(values[0]);
    values[1] = (float) Math.toDegrees(values[1]);
    values[2] = (float) Math.toDegrees(values[2]);

    int orientation = mActivity.getRequestedOrientation();

    if ((-10.0f < values[1] && values[1] <= 10f) && values[2] < -40f) {// 向左
        if (orientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                && (mPortraitOrLandscape == null || !mPortraitOrLandscape)) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setButtonChecked(true);
        }

        if (mPortraitOrLandscape != null && !mPortraitOrLandscape)
            mPortraitOrLandscape = null;

    } else if (40.0f < values[1] && values[1] < 90.0f) { // 向下
        if (orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                && (mPortraitOrLandscape == null || mPortraitOrLandscape)) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            setButtonChecked(false);
        }

        if (mPortraitOrLandscape != null && mPortraitOrLandscape)
            mPortraitOrLandscape = null;

    } else if (-90.0f < values[1] && values[1] < -40.0f) { // 向上
        if (orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                && (mPortraitOrLandscape == null || mPortraitOrLandscape)) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setButtonChecked(false);
        }

        if (mPortraitOrLandscape != null && mPortraitOrLandscape)
            mPortraitOrLandscape = null;

    } else if ((-10.0f < values[1] && values[1] <= 10f) && values[2] > 40f) { // 向右
        if (orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
                && (mPortraitOrLandscape == null || !mPortraitOrLandscape)) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            setButtonChecked(true);
        }

        if (mPortraitOrLandscape != null && !mPortraitOrLandscape)
            mPortraitOrLandscape = null;
    }
}

@Override
public void onSensorChanged(SensorEvent event) {
    switch (event.sensor.getType()) {
        case Sensor.TYPE_MAGNETIC_FIELD:
            mMagneticFieldValues = event.values;
            break;

        case Sensor.TYPE_ACCELEROMETER:
            mAccelerometerValues = event.values;
            break;

        default:
            break;
    }

    calculateOrientation();
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}

具体的使用很简单,贴上伪代码如下。

public class PlayVideoActivity extends Activity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initData();
    initView();
}

   private void initView(){
    }

//初始化界面
private void initView() {
    setContentView(R.layout.xxxx);
    mScreenOrientationHelper = new ScreenOrientationHelper(this, mFullscreenButton, mFullscreenFullButton);
}

@Override
public void onBackPressed() {
    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT) {
        mScreenOrientationHelper.portrait();
        return;
    }
   
}
@Override
protected void onStart() {
    super.onStart();
    mScreenOrientationHelper.postOnStart();
}



@Override
protected void onStop() {
    super.onStop();
    mScreenOrientationHelper.postOnStop();
}



@Override
public void onConfigurationChanged(Configuration newConfig) {
    mOrientation = newConfig.orientation;
    onOrientationChanged();
    super.onConfigurationChanged(newConfig);
}
   private void onOrientationChanged() {
    //这里实现横屏和竖屏的ui显示,隐藏和显示你想要展现的布局即可,注意需要状态栏以及布局的约束。代码就不写了。
}

}

你可能感兴趣的:(Android横竖屏手动以及自动切换的实现)