Android Webview中的视屏全屏功能实现

作者: 燊在锦官城 | 来源:发表于2016-07-04 16:14 被阅读2534次
    Webview中的视屏全屏的时候是会出现问题的,我们对其做如下的修改:
    1. 设置webview的WebChromeClient
    WebSettings webSettings = mWebview.getSettings();
    mWebview.setWebChromeClient(new MyWebChromeClient());
    
    1. 重写WebChromeClient的onShowCustomView和onHideCustomView方法
        class MyWebChromeClient extends WebChromeClient {
            private View myView = null;
    
            // 全屏
            @Override
            public void onShowCustomView(View view, CustomViewCallback callback) {
                super.onShowCustomView(view, callback);
    
                ViewGroup parent = (ViewGroup) mWebview.getParent();
                parent.removeView(mWebview);
    
                // 设置背景色为黑色
                view.setBackgroundColor(mActivity.getResources().getColor(R.color.black));
                parent.addView(view);
                myView = view;
    
                setFullScreen();
                titleBar.setVisibility(View.GONE);
    
            }
    
            // 退出全屏
            @Override
            public void onHideCustomView() {
                super.onHideCustomView();
                if (myView != null) {
    
                    ViewGroup parent = (ViewGroup) myView.getParent();
                    parent.removeView(myView);
                    parent.addView(mWebview);
                    myView = null;
    
                    titleBar.setVisibility(View.VISIBLE);
                    quitFullScreen();
                }
            }
        }
         /**
         * 设置全屏
         */
        private void setFullScreen() {
            // 设置全屏的相关属性,获取当前的屏幕状态,然后设置全屏
            mActivity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    
        /**
         * 退出全屏
         */
        private void quitFullScreen() {
            // 声明当前屏幕状态的参数并获取
            final WindowManager.LayoutParams attrs = mActivity.getWindow().getAttributes();
            attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mActivity.getWindow().setAttributes(attrs);
            mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    

    以上。

    参考:

    http://blog.csdn.net/lygscg123/article/details/38084769

    我的博客(第一时间更新):

    http://huangss.farbox.com/

    相关文章

      网友评论

      本文标题:Android Webview中的视屏全屏功能实现

      本文链接:https://www.haomeiwen.com/subject/xuacjttx.html