记录一个播放视频页面的旋转屏处理封装类.
/**
* Created by sven on 2017/8/7.
* 处理当前activity转屏事件
*/
open class OrientationHelper {
var mActivity: Activity
var mOrientationListener:OrientationListener
var mOrientationObserver:DataObserver
var sensorEnable=true
var settingEnable=false
constructor(activity:Activity){
mActivity=activity
settingEnable = isSettingRotationEnable()
mOrientationListener = object : OrientationListener(mActivity) {
override fun onPortrait() {
sensorEnable = true
}
override fun onLandscape() {
if (settingEnable && sensorEnable) {
setBySensor()
}
}
}
mOrientationObserver = object : DataObserver(mActivity.contentResolver, Settings.System.ACCELEROMETER_ROTATION) {
override fun onChange() {
settingEnable = isSettingRotationEnable()
if (settingEnable) {
setBySensor()
} else {
setPortrait()
}
}
}
}
/**
* 获取系统设置是否允许转屏
* @return
*/
fun isSettingRotationEnable(): Boolean {
val `in` = Settings.System.getInt(mActivity.contentResolver, Settings.System.ACCELEROMETER_ROTATION, 0)
return `in` == 1
}
fun onBackPressed() {
if (isLandscape()) {
setPortrait()
} else {
mActivity.finish()
}
}
fun enable() {
mOrientationListener.enable()
mOrientationObserver.startObserver()
}
fun disable() {
mOrientationListener.disable()
mOrientationObserver.stopObserver()
}
fun rotation() {
if (isPortrait()) {
setLandscape()
} else {
setPortrait()
}
}
fun setLock() {
mActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED
disable()
}
fun setUnLock() {
setBySensor()
enable()
}
fun setPortrait() {
sensorEnable = false
if (mActivity.requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
mActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
fun setLandscape() {
if (mActivity.requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
mActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
}
fun setBySensor() {
if (mActivity.requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_SENSOR) {
mActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
}
fun isPortrait(): Boolean {
return mActivity.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT
}
fun isLandscape(): Boolean {
return mActivity.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
}
fun isLocked():Boolean{
return mActivity.requestedOrientation==ActivityInfo.SCREEN_ORIENTATION_LOCKED
}
/**
* 监听屏幕当前角度
*/
abstract class OrientationListener(context: Context) : OrientationEventListener(context) {
abstract fun onLandscape()
abstract fun onPortrait()
override fun onOrientationChanged(orientation: Int) {
// 竖屏
if (orientation in 0..30 || orientation >= 330
|| orientation in 150..210) {
onPortrait()
//横屏
} else if (orientation in 90..120 || orientation in 240..300) {
onLandscape()
}
}
}
/**
* 监听系统设置修改
*/
abstract class DataObserver : ContentObserver {
internal abstract fun onChange()
internal var mResolver: ContentResolver
internal var action: String? = null
internal var mUri: Uri?=null
constructor(contentResolver: ContentResolver, action: String) : super(null) {
mResolver = contentResolver
this.action = action
}
constructor(resolver: ContentResolver, uri: Uri) : super(null) {
mResolver = resolver
this.mUri = uri
}
//屏幕旋转设置改变时调用
override fun onChange(selfChange: Boolean) {
super.onChange(selfChange)
onChange()
}
fun startObserver() {
if (null != action) {
mResolver.registerContentObserver(Settings.System
.getUriFor(action), false,
this)
} else {
mResolver.registerContentObserver(mUri, false, this)
}
}
fun stopObserver() {
mResolver.unregisterContentObserver(this)
}
}
满足了我在项目中的需求: 在设置转屏开关开启的时候自动转屏,用户可点击横竖屏切换,可在锁定当前屏幕方向,自动处理后退返回键.用起来很方便,我比较满意,不知道还有其他更好的方案没有?