webView 支持全屏播放
直接上代码
public class CustomFullScreenWebViewClient extends WebChromeClient {
WebView webView;
Context context;
/**
* 视频全屏参数
*/
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;
int mOriginalSystemUiVisibility;
public CustomFullScreenWebViewClient(Context context, WebView webViewParam) {
this.webView = webViewParam;
this.context = context;
}
/**
* 视频播放相关的方法
*/
@Override
public View getVideoLoadingProgressView() {
FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
// 添加慢慢弹出来的动画效果
AnimationSet animSet = new AnimationSet(true);
AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f);
fadeIn.setDuration(800);
animSet.addAnimation(fadeIn);
return frameLayout;
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
showCustomView(view, callback);
}
@Override
public void onHideCustomView() {
hideCustomView();
}
/**
* 视频播放全屏
**/
private void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (customView != null) {
callback.onCustomViewHidden();
return;
}
((Activity) context).getWindow().getDecorView();
FrameLayout decor = (FrameLayout) ((Activity) context).getWindow().getDecorView();
mOriginalSystemUiVisibility = decor.getSystemUiVisibility();
fullscreenContainer = new FrameLayoutFullscreenHolder(context);
fullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
decor.addView(fullscreenContainer, COVER_SCREEN_PARAMS);
customView = view;
setStatusBarVisibility(false);
customViewCallback = callback;
// 隐藏系统UI
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decor.setSystemUiVisibility(uiOptions);
// 添加慢慢弹出来的动画效果
AnimationSet animSet = new AnimationSet(true);
AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f);
fadeIn.setDuration(800);
animSet.addAnimation(fadeIn);
view.startAnimation(animSet);
}
/**
* 隐藏视频全屏
*/
private void hideCustomView() {
FrameLayout decor = (FrameLayout) ((Activity) context).getWindow().getDecorView();
// 恢复系统UI的可见性
decor.setSystemUiVisibility(mOriginalSystemUiVisibility);
if (customView == null) {
return;
}
setStatusBarVisibility(true);
StatusBarCompat.setStatusBarDarkFont((Activity) context, true); // 状态栏字体颜色
decor.removeView(fullscreenContainer);
fullscreenContainer = null;
customView = null;
// 通知全屏播放已结束
customViewCallback.onCustomViewHidden();
}
/**
* 全屏容器界面
*/
static class FrameLayoutFullscreenHolder extends FrameLayout {
public FrameLayoutFullscreenHolder(Context ctx) {
super(ctx);
setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
}
@Override
public boolean onTouchEvent(MotionEvent evt) {
return true;
}
}
private void setStatusBarVisibility(boolean visible) {
int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
((Activity) context).getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}