X5webview完美去掉分享功能和缓存功能(2)

 前段时间比较忙,没有来得及写完如何将X5WEBVIEW分享功能和缓存功能屏蔽,下面直接来干货,上代码。

1.首先在布局文件中增加一个全屏的布局,

 <!-- 视频全屏-->
    <FrameLayout
        android:id="@+id/video_fullView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/black"
        android:visibility="gone">
         
    </FrameLayout>

 

2.其次初始化x5webview的时候务必要配置为标准全屏

if (webView.getX5WebViewExtension() != null) {
            Bundle data = new Bundle();
            data.putBoolean("standardFullScreen", true);// true表示标准全屏,false表示X5全屏;不设置默认false,
            data.putBoolean("supportLiteWnd", false);// false:关闭小窗;true:开启小窗;不设置默认true,
            data.putInt("DefaultVideoScreen", 1);// 1:以页面内开始播放,2:以全屏开始播放;不设置默认:1
            webView.getX5WebViewExtension().invokeMiscMethod("setVideoParams", data);
        }

 

3.设置进入和退出全屏的事件 

            // 拦截全屏调用的方法
            @Override
            public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback callback) {
                super.onShowCustomView(view, callback);
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                webView.setVisibility(View.INVISIBLE);
                // 如果一个视图已经存在,那么立刻终止并新建一个
                if (xCustomView != null) {
                    callback.onCustomViewHidden();
                    return;
                }
                try {
            //下面为了兼容部分手机顶部白色区域块 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); view.setVisibility(View.VISIBLE); video_fullView.addView(view);              xCustomView = view; xCustomView.setVisibility(View.VISIBLE); xCustomViewCallback = callback; video_fullView.setVisibility(View.VISIBLE); //下面切勿改动,如果x5webview出现改动,下面代码将不能生效,需要重新分析, if(view instanceof ViewGroup && ((ViewGroup) view).getChildCount() > 1){ View root_child1 = (((ViewGroup) view).getChildAt(1)); if(root_child1 instanceof ViewGroup && ((ViewGroup) root_child1).getChildCount() > 2){ View root_child1_child1 = ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(1))); View root_child1_child2 = ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(2))); if(root_child1_child1 instanceof ViewGroup && ((ViewGroup) root_child1_child1).getChildCount() > 1){ View root_child1_child1_child1 = (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(1)))).getChildAt(1)); if(root_child1_child1_child1 instanceof ViewGroup && ((ViewGroup) root_child1_child1_child1).getChildCount() > 5){ ((ViewGroup) (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(1)))).getChildAt(1))).getChildAt(3).setVisibility(View.GONE); ((ViewGroup) (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(1)))).getChildAt(1))).getChildAt(5).setVisibility(View.GONE); } } if(root_child1_child2 instanceof ViewGroup && ((ViewGroup) root_child1_child2).getChildCount() > 1){ View root_child1_child2_child1 = (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(2)))).getChildAt(1)); if(root_child1_child2_child1 instanceof ViewGroup && ((ViewGroup) root_child1_child2_child1).getChildCount() > 5){ ((ViewGroup) (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(2)))).getChildAt(1))).getChildAt(3).setVisibility(View.GONE); ((ViewGroup) (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(2)))).getChildAt(1))).getChildAt(5).setVisibility(View.GONE); } } //处理x5webview播放器上横竖屏按钮事件 View root_child1_child3 = ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(3))); if(root_child1_child3 instanceof ViewGroup && ((ViewGroup) root_child1_child3).getChildCount() > 0){ View root_child1_child3_child0 = (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(3)))).getChildAt(0)); if(root_child1_child3_child0 instanceof ViewGroup && ((ViewGroup) root_child1_child3_child0).getChildCount() > 4){ ((ViewGroup) root_child1_child3_child0).getChildAt(4).setVisibility(View.INVISIBLE); ((ViewGroup) root_child1_child3_child0).getChildAt(4).setEnabled(false); ((ViewGroup) root_child1_child3_child0).getChildAt(4).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } }); } }                  //处理x5webview播放器上横竖屏按钮事件 if(root_child1_child2 instanceof ViewGroup && ((ViewGroup) root_child1_child2).getChildCount() > 1){ View root_child1_child2_child0 = (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(2)))).getChildAt(0)); if(root_child1_child2_child0 instanceof ViewGroup && ((ViewGroup) root_child1_child2_child0).getChildCount() > 4){ ((ViewGroup) (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(2)))).getChildAt(0))).getChildAt(4).setVisibility(View.INVISIBLE); ((ViewGroup) (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(2)))).getChildAt(0))).getChildAt(4).setEnabled(false); ((ViewGroup) (((ViewGroup) ((((ViewGroup) (((ViewGroup) view).getChildAt(1))).getChildAt(2)))).getChildAt(0))).getChildAt(4).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } }); } } } } } catch (Exception exception) { exception.printStackTrace(); } finally { } } @Override public void onHideCustomView() { super.onHideCustomView(); if (xCustomView == null) { // 不是全屏播放状态 return; }try { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); StatusBarCompat.setStatusBarColor(NewsContentViewActivityV3.this, getResources().getColor(R.color.article_statusbar)); xCustomView.setVisibility(View.GONE); video_fullView.removeView(xCustomView); xCustomView = null; video_fullView.setVisibility(View.GONE); xCustomViewCallback.onCustomViewHidden(); webView.setVisibility(View.VISIBLE); } catch (Exception exception) { exception.printStackTrace(); } finally { } }

主要的思路:使用自定义全屏,可以监听到进入和退出全屏的事件,并且拿到对应的view,这样就可以进行相关的处理。并且通过对x5webview内核的分析,可以精准对相关的按钮和动作进行设置,屏蔽比如分享和缓存功能,按照需要可以添加横竖屏的相应动作。

总结:如果x5webview本身就处理好了标准全屏,就没我们啥事了。

posted @ 2018-10-25 10:41  那些人追过的年  阅读(3865)  评论(0编辑  收藏  举报