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

 前段時間比較忙,沒有來得及寫完如何將X5WEBVIEW分享功能和緩存功能屏蔽,下面直接來乾貨,上代碼。android

1.首先在佈局文件中增長一個全屏的佈局,web

 <!-- 視頻全屏-->
    <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.設置進入和退出全屏的事件 ide

            // 攔截全屏調用的方法
            @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自己就處理好了標準全屏,就沒咱們啥事了。this

相關文章
相關標籤/搜索