设置WebChromeClient
设置后,重点重下以下两个方法
showCustomView会将webView中视频的部分的view传递进来,我们需要提供一个ViewGoup容器接收并显示它
onHideCustomView则在用户退出全屏时调用,我们需要将它从自己的ViewGoup容器中移除
override fun onShowCustomView(view: View, callback: CustomViewCallback?) {
LogUtils.i("全屏展示")
showCustomView(view, callback)
super.onHideCustomView()
}
override fun onHideCustomView() {
LogUtils.i("全屏隐藏")
onHideCustomView()
hideCustomView()
}
适配代码
var mCustomView: View? = null
/**
**方案一
**竖屏,View旋转
**/
private fun showCustomView(view: View, callback: WebChromeClient.CustomViewCallback?) {
if (mCustomView != null) {
callback?.onCustomViewHidden()
return
}
val act = ActivityUtils.getTopActivity()
BarUtils.setStatusBarVisibility(act.window, false)
val decorView: FrameLayout = ActivityUtils.getTopActivity().window.decorView as FrameLayout
//黑色背景
view.setBackgroundColor(Color.BLACK)
//减去状态栏部分,避免被状态栏遮挡
decorView.addView(view, decorView.height,decorView.width)
mCustomView = view
//横屏旋转对齐
view.rotation = 90f
view.translationX = (decorView.width-decorView.height).toFloat()/2f
view.translationY = (decorView.height-decorView.width).toFloat()/2f
}
/**
**方案二
**屏幕旋转,要加上配置android:configChanges="keyboardHidden|orientation|screenSize"避免view重建
**/
private fun showCustomView(view: View, callback: WebChromeClient.CustomViewCallback?) {
if (mCustomView != null) {
callback?.onCustomViewHidden()
return
}
view.setBackgroundColor(Color.BLACK)
val act = ActivityUtils.getTopActivity()
BarUtils.setStatusBarVisibility(act.window, false)
val decorView: FrameLayout = ActivityUtils.getTopActivity().window.decorView as FrameLayout
decorView.addView(view, decorView.height,decorView.width)
mCustomView = view
//改变屏幕方向,隐藏时记得还原
act.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
private fun hideCustomView(){
if (mCustomView == null) {
return
}
val act = ActivityUtils.getTopActivity()
val decorView: FrameLayout = act.window.decorView as FrameLayout
//act.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
decorView.setBackgroundColor(Color.TRANSPARENT)
decorView.removeView(mCustomView)
mCustomView = null
BarUtils.setStatusBarVisibility(act.window, true)
}