19-9-1-android强制转屏

在项目中因为有个播放界面用到了强制转屏,就记录以下,顺便在将 自动转屏也尝试弄出来,一起做个记录;

强制转屏:

两个方法:

​ 1:在Activity的onCreate()中设置:

竖屏:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

横屏:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

默认:


setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

​ 2:.在AndroidManifest.xml对每个Activity进行配置+xml添加设置:

androidMainfest.xml

<activity android:name=".MainActivity"
            android:configChanges="orientation|keyboard">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

xml

android:screenOrientation="landscape"横屏设置;
android:screenOrientation="portrait"竖屏设置;
转屏数据处理

​ 手动切换横屏和竖屏后,由于长宽的变化数据就会产生变化;

​ 1,layout-land和layout-port

​ 在res目录下建立两个目录分别是layout—land(横屏数据) layout-port(竖屏的layout)相应的文件不变,android模拟器会自动通过横竖屏找到相对应的layout

​ 2.onCreate()中判断横竖屏

​ 通过this.getResources().getConfiguration().orientation判断当前是横屏还是竖屏,然后加载相应的xml布局文件。因为当屏幕变为横屏的时候,系统会重新呼叫当前Activity的OnCreate方法,你可以把以下方法放在你的 OnCreate中来检查当前的方向,然后可以让你的SetContentView来载入不同的Layout xml.

if (this.getResources().getConfiguration().orientation==
    Configuration.ORIENTATION_LANDSCA{		
        Log.i("info", "landscape"); 
}elseif(this.getResources().getConfiguration().orientation==
    Configuration.ORIENTATION_ORTRAIT) { 
		Log.i("info", "portrait");
}

​ 3.这是最后一个也是最重要的一个,由于项目是视频不想在转化的时候出现黑屏和内容重新播放的情况,所以结合OrientationEventListener,自定义旋转监听设置;

​ 首先,创建OrientationEventListener对象:

private OrientationEventListener mOrientationListener;
// screen orientation listener
private boolean mScreenProtrait = true;
private boolean mCurrentOrient = false;

​ 自定义回调接口:

abstract protected void OrientationChanged(int orientation);

​ 自定义监听类:

private final void startOrientationChangeListener() {
 mOrientationListener = new OrientationEventListener(this) {
 	@Override
 	public void onOrientationChanged(int rotation) {
 		if (((rotation >= 0) && (rotation <= 45)) || (rotation >= 315)||	((rotation>=135)&&(rotation<=225))) {//portrait
 			mCurrentOrient = true;
 				if(mCurrentOrient!=mScreenProtrait){
 					mScreenProtrait = mCurrentOrient;
 					OrientationChanged(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
 					Log.d(TAG, "Screen orientation changed from Landscape to Portrait!");
 				}
 		}
 else if (((rotation > 45) && (rotation < 135))||((rotation>225)&&(rotation<315))){//landscape
 		mCurrentOrient = false;
 			if(mCurrentOrient!=mScreenProtrait){
 				mScreenProtrait = mCurrentOrient;
 				OrientationChanged(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
 				Log.d(TAG, "Screen orientation changed from Portrait to Landscape!");
 			}
 }
 }
 };
 mOrientationListener.enable();
}

​ 在onCreate()中调用:

startOrientationChangeListener();
生命周期

启动一个Activity,

onCreate()

onStart()

onResume()

竖屏切换到横屏:(调用一次生命周期)

onSaveInstanceState()

onPause()

onStop()

onDestroy()

onCreate()

onStart()

onRestoreInstanceState()

onResume()

横屏切换到竖屏:(调用两次生命周期)

onSaveInstanceState()

onPause()

onStop()

onDestroy()

onCreate()

onStart()

onRestoreInstanceState()

onResume()

onSaveInstanceState()

onPause()

onStop()

onDestroy()

onCreate()

onStart()

onRestoreInstanceState()

onResume()

设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次。

设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法。

关于通过手机内置陀螺仪转屏还没有弄明白,如果搞清楚会在开一篇新的内容记录;

感谢

https://blog.csdn.net/JugglerTao/article/details/51782801

https://blog.csdn.net/dj0379/article/details/50906843

欢迎提意见 :jsntwangchenchen@outlook.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值