webview支持视频全屏播放以及方向随重力感应自适应变化,超实用,已经过测试准行!准行!准行!
直接上代码:
public class CustomWebView extends WebView {
private boolean mIsFullScreen = false;
private Context mActivity;
private ProgressBar mProgressBar;
private int perScreenStatus = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 记录视频播放前屏幕状态,默认竖屏
/**
* 是否使用默认的WebSettings相关设置
*/
private boolean defaultSetting = false;
/**
* 页面加载是否显示进度条, 默认不显示
*/
private boolean showProgressBar = false;
/**
* 视频播放是否可全屏播放,默认是
*/
private boolean supportFullScreenPlayVideo = true;
/**
* 视频全屏播放时屏幕方向 默认 设置页面方向随重力感应自适应变化
*/
private int videoScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
/**
* 视频全屏参数
*/
protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
private View customView;
private FrameLayout fullscreenContainer;
private WebChromeClient.CustomViewCallback customViewCallback;
public CustomWebView(@NonNull Context context) {
super(context);
mActivity = context;
initView();
}
public CustomWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mActivity = context;
//属性
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomWebView);
defaultSetting = mTypedArray.getBoolean(R.styleable.CustomWebView_defaultSetting, defaultSetting);
showProgressBar = mTypedArray.getBoolean(R.styleable.CustomWebView_showProgressBar, showProgressBar);
supportFullScreenPlayVideo = mTypedArray.getBoolean(R.styleable.CustomWebView_supportFullScreenPlayVideo, supportFullScreenPlayVideo);
int gravityType = mTypedArray.getInt(R.styleable.CustomWebView_defaultSetting, videoScreenOrientation);
switch (gravityType) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
videoScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
videoScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case ActivityInfo.SCREEN_ORIENTATION_SENSOR:
videoScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
break;
}
initView();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mIsFullScreen) {
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMea