自定义带进度条的WebView导致的问题,并且参考代码实现webiew加载视频全屏播放

布局文件

<?xml version=”1.0″ encoding=”utf-8″?>

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="@dimen/base_3dp"
    android:progressDrawable="@drawable/progressbar" />

<WebView
    android:id="@+id/mWebView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

public class HtmlWebView extends LinearLayout { private ProgressBar progressView;//进度条 private Context context; private WebView mWebView;

public WebView getWebView() {
    return mWebView;
}

public ProgressBar getProgressView() {
    return progressView;
}

public HtmlWebView(Context context) {
    this(context,null);
    this.context = context;

}
public HtmlWebView(Context context, AttributeSet attrs) {
    this(context, attrs,0);
    this.context = context;
}
public HtmlWebView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    init();

}




private void init() {
    //初始化进度条
    View view = LayoutInflater.from(context).inflate(R.layout.base_progress_layout_horizontal, this,false);
    mWebView = view.findViewById(R.id.mWebView);
    progressView=view.findViewById(R.id.progressBar);
    //把进度条加到Webview中
    addView(view);
}

}

代码
public class WebViewActivity extends BaseActivity<IWebViewView,WebViewPresenterImpl> {

private HtmlWebView mWebView;
private TextView tvTitle;
private String title;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);
    tvTitle = findViewById(R.id.tvTitle);
    mWebView = findViewById(R.id.webView);
    Intent intent = getIntent();
    title = intent.getStringExtra("title");
    if (title!=null) {
        tvTitle.setText(title);
    }
    settingWebNew();
}


@SuppressLint("SetJavaScriptEnabled")
private void settingWebNew(){
    WebSettings settings = mWebView.getWebView().getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setUseWideViewPort(true);
    // 设置加载进来的页面自适应手机屏幕
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        settings.setMediaPlaybackRequiresUserGesture(false);
    }
    settings.setPluginState(WebSettings.PluginState.ON);
    settings.setRenderPriority(WebSettings.RenderPriority.HIGH); // 提高渲染的优先级
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    settings.setPluginState(WebSettings.PluginState.ON);
    settings.setDomStorageEnabled(true);// 必须保留,否则无法播放优酷视频,其他的O
    mWebView.getWebView().loadUrl(TextUtils.isEmpty(getIntent().getStringExtra("url")) ?"https://www.hao123.com/":getIntent().getStringExtra("url"));

}
private View customView;
private FrameLayout fullscreenContainer;
private WebChromeClient.CustomViewCallback customViewCallback;
/** 视频播放全屏 **/
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;
    }
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏
    this.getWindow().getDecorView();
    FrameLayout decor = (FrameLayout) getWindow().getDecorView();
    fullscreenContainer = new FullscreenHolder(WebViewActivity.this);
    fullscreenContainer.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    decor.addView(fullscreenContainer, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    customView = view;
    customViewCallback = callback;
}
/** 隐藏视频全屏 */
private void hideCustomView() {
    if (customView == null) {
        return;
    }
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置竖屏
    FrameLayout decor = (FrameLayout) getWindow().getDecorView();
    decor.removeView(fullscreenContainer);
    fullscreenContainer = null;
    customView = null;
    customViewCallback.onCustomViewHidden();
    mWebView.setVisibility(View.VISIBLE);
}

/** 全屏容器界面 */
static class FullscreenHolder extends FrameLayout {

    public FullscreenHolder(Context ctx) {
        super(ctx);
        setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
    }

    @Override
    public boolean onTouchEvent(MotionEvent evt) {
        return true;
    }
}

@Override
public void onConfigurationChanged(Configuration config) {
    super.onConfigurationChanged(config);
    switch (config.orientation) {
        case Configuration.ORIENTATION_LANDSCAPE:
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            break;
        case Configuration.ORIENTATION_PORTRAIT:
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            break;
    }
}


@Override
protected WebViewPresenterImpl createPresent() {
    return new WebViewPresenterImpl();
}

@Override
public void initEvent() {
    findViewById(R.id.ivBack).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });

    mWebView.getWebView().setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);


        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

    });
    mWebView.getWebView().setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if (newProgress == 100) {
                //加载完毕进度条消失
                mWebView.getProgressView().setVisibility(View.GONE);
            } else {
                //更新进度
                mWebView.getProgressView().setProgress(newProgress);
            }
        }
        /*** 视频播放相关的方法 **/
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            tvTitle.setText(title);
        }


        @Override
        public View getVideoLoadingProgressView() {
            FrameLayout frameLayout = new FrameLayout(WebViewActivity.this);
            frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return frameLayout;
        }

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            showCustomView(view, callback);
        }

        @Override
        public void onHideCustomView() {
            hideCustomView();
        }

    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mWebView != null) {
        // 如果先调用destroy()方法,则会命中if (isDestroyed()) return;这一行代码,需要先onDetachedFromWindow(),再
        mWebView.getWebView().stopLoading();
        // 退出时调用此方法,移除绑定的服务,否则某些特定系统会报错
        mWebView.getWebView().getSettings().setJavaScriptEnabled(false);
        mWebView.getWebView().clearHistory();
        mWebView.getWebView().clearView();
        mWebView.removeAllViews();
        mWebView.getWebView().destroy();
    }
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            /** 回退键 事件处理 优先级:视频播放全屏-网页回退-关闭页面 */
            if (customView != null) {
                hideCustomView();
            } else if (mWebView.getWebView().canGoBack()) {
                mWebView.getWebView().goBack();
            } else {
                finish();
            }
            return true;
        default:
            return super.onKeyUp(keyCode, event);
    }
}

}
由于刚开始设置
View view = LayoutInflater.from(context).inflate(R.layout.base_progress_layout_horizontal, null);导致WebView加载出来的界面没有显示出正常的高度,被变形压缩了
LayoutInflater.from(this).inflate()参数解析:https://blog.csdn.net/fesdgasdgasdg/article/details/72870280

点赞