之前項(xiàng)目做攝像頭的采集視頻播放斥赋,里面有關(guān)于橫豎屏的顯示內(nèi)容缰猴,這里把實(shí)現(xiàn)的方法給大家展示出來。
首先創(chuàng)建橫豎屏監(jiān)聽類疤剑,需要傳入當(dāng)前activity滑绒,同時(shí)提供了傳入button來實(shí)現(xiàn)手動全屏和退出全屏的操作闷堡。
ScreenOrientationHelper監(jiān)聽類
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() {
//這里實(shí)現(xiàn)橫屏和豎屏的ui顯示杠览,隱藏和顯示你想要展現(xiàn)的布局即可,注意需要狀態(tài)欄以及布局的約束纵势。代碼就不寫了踱阿。
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者