記錄一個(gè)播放視頻頁(yè)面的旋轉(zhuǎn)屏處理封裝類.
/**
* Created by sven on 2017/8/7.
* 處理當(dāng)前activity轉(zhuǎn)屏事件
*/
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()
}
}
}
}
/**
* 獲取系統(tǒng)設(shè)置是否允許轉(zhuǎn)屏
* @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
}
/**
* 監(jiān)聽(tīng)屏幕當(dāng)前角度
*/
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()
}
}
}
/**
* 監(jiān)聽(tīng)系統(tǒng)設(shè)置修改
*/
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
}
//屏幕旋轉(zhuǎn)設(shè)置改變時(shí)調(diào)用
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)
}
}
滿足了我在項(xiàng)目中的需求: 在設(shè)置轉(zhuǎn)屏開(kāi)關(guān)開(kāi)啟的時(shí)候自動(dòng)轉(zhuǎn)屏,用戶可點(diǎn)擊橫豎屏切換,可在鎖定當(dāng)前屏幕方向,自動(dòng)處理后退返回鍵.用起來(lái)很方便,我比較滿意,不知道還有其他更好的方案沒(méi)有?