美文网首页
video 横屏写法onShowCustomView onHid

video 横屏写法onShowCustomView onHid

作者: cain07 | 来源:发表于2022-03-13 15:07 被阅读0次

    webview 视频横屏 方法

    1.

    myWebView = (WebView) findViewById(R.id.webView1);
    
    private MyWebChromeClient mWebChromeClient = null;
    private View mCustomView;
    private RelativeLayout mContentView;
    private FrameLayout mCustomViewContainer;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    
    private class MyWebChromeClient extends WebChromeClient {
        FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
    
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            // if a view already exists then immediately terminate the new one
            if (mCustomView != null) {
                callback.onCustomViewHidden();
                return;
            }
            mContentView = (RelativeLayout) findViewById(R.id.activity_main);
            mContentView.setVisibility(View.GONE);
            mCustomViewContainer = new FrameLayout(MainActivity.this);
            mCustomViewContainer.setLayoutParams(LayoutParameters);
            mCustomViewContainer.setBackgroundResource(android.R.color.black);
            view.setLayoutParams(LayoutParameters);
            mCustomViewContainer.addView(view);
            mCustomView = view;
            mCustomViewCallback = callback;
            mCustomViewContainer.setVisibility(View.VISIBLE);
            setContentView(mCustomViewContainer);
        }
    
        @Override
        public void onHideCustomView() {
            if (mCustomView == null) {
                return;
            } else {
                // Hide the custom view.  
                mCustomView.setVisibility(View.GONE);
                // Remove the custom view from its container.  
                mCustomViewContainer.removeView(mCustomView);
                mCustomView = null;
                mCustomViewContainer.setVisibility(View.GONE);
                mCustomViewCallback.onCustomViewHidden();
                // Show the content view.  
                mContentView.setVisibility(View.VISIBLE);
                setContentView(mContentView);
            }
        }
    }
    
    @Override
    public void onBackPressed() {
        if (mCustomViewContainer != null)
            mWebChromeClient.onHideCustomView();
        else if (myWebView.canGoBack())
            myWebView.goBack();
        else
            super.onBackPressed();
    }
    
    

    2.

    new WebViewClient() {
    
        @Override
        public void onLoadResource(WebView view, String url) {
    
            if (url.startsWith("http://www.youtube.com/get_video_info?")) {
                try {
                    String path = url.replace("http://www.youtube.com/get_video_info?", "");
    
                    String[] parqamValuePairs = path.split("&");
    
                    String videoId = null;
    
                    for (String pair : parqamValuePairs) {
                        if (pair.startsWith("video_id")) {
                            videoId = pair.split("=")[1];
                            break;
                        }
                    }
    
                    if(videoId != null){
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com"))
                                .setData(Uri.parse("http://www.youtube.com/watch?v=" + videoId)));
                        needRefresh = true;
    
                        return;
                    }
                } catch (Exception ex) {
                }
            } else {
                super.onLoadResource(view, url);
           }
        }
    }
    

    3.

    WebChromeClient webChromeClient = new WebChromeClient() {
                private ViewGroup rootView;
                private WebChromeClient.CustomViewCallback customViewCallback;
    
                @Override
                public void onShowCustomView(View view, CustomViewCallback callback) {
    
                    //Destroy full screen view if already exists
                    if (fullscreenView != null) {
                        callback.onCustomViewHidden();
                        return;
                    }
    
                    //Layout params to fit fullscreen view in our activity
                    ViewGroup.LayoutParams layoutParams = 
                            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                    ViewGroup.LayoutParams.MATCH_PARENT);
                    //Catch root of current activity to add fullscreen view
                    rootView = (ViewGroup) WebViewBaseActivity.this.webView.getRootView();
                    
                    //Store full screen view, we need it to destroy it out of scope
                    fullscreenView = view;
    
                    customViewCallback = callback;
                    rootView.addView(fullscreenView, layoutParams);
                }
    
                @Override
                public void onHideCustomView() {
                    //Make sure fullscreen view exists
                    if (fullscreenView != null) {
                        
                        //Remove fullscreen view from activity root view
                        rootView.removeView(fullscreenView);
                        fullscreenView = null;
    
                        //Tell browser we did remove fullscreen view
                        customViewCallback.onCustomViewHidden();
                    }
                }
    
    @Override
    public void onBackPressed() {
        if (fullscreenView != null) {
            webChromeClient.onHideCustomView();
        }
    }    
    

    4.

    https://github.com/Happyandhappy/maginsoftteam-chromecase-Android/blob/e4dd1bca1f4d9c959a26fe7b561fb352f77552af/app/src/main/java/com/oxycast/chromecastapp/web/WebChromeClientEx.java

    public class WebChromeClientEx extends WebChromeClient {
    
    
        // Fullscreen
        private View mCustomView;
        private WebChromeClient.CustomViewCallback mCustomViewCallback;
        private int mOriginalOrientation;
    
        private FrameLayout mFullscreenContainer;
    
        private final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    
    
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                if (mCustomView != null) {
                    callback.onCustomViewHidden();
                    return;
                }
    
                mOriginalOrientation = ((Activity)context).getRequestedOrientation();
                FrameLayout decor = (FrameLayout) ((Activity)context).getWindow().getDecorView();
                mFullscreenContainer = new FullscreenHolder(context);
                mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
                decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
                mCustomView = view;
                setFullscreen(true);
                mCustomViewCallback = callback;
    //          mActivity.setRequestedOrientation(requestedOrientation);
            }
    
            super.onShowCustomView(view, callback);
        }
    
        @SuppressWarnings("deprecation")
        @Override
        public void onShowCustomView(View view, int requestedOrientation, WebChromeClient.CustomViewCallback callback) {
            this.onShowCustomView(view, callback);
        }
    
        @Override
        public void onHideCustomView() {
            if (mCustomView == null) {
                return;
            }
    
            setFullscreen(false);
            FrameLayout decor = (FrameLayout) ((Activity)context).getWindow().getDecorView();
            decor.removeView(mFullscreenContainer);
            mFullscreenContainer = null;
            mCustomView = null;
            mCustomViewCallback.onCustomViewHidden();
            ((Activity)context).setRequestedOrientation(mOriginalOrientation);
        }
    
        private void setFullscreen(boolean enabled) {
            Window win = ((Activity)context).getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
            if (enabled) {
                winParams.flags |= bits;
            } else {
                winParams.flags &= ~bits;
                if (mCustomView != null) {
                    mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                }
            }
            win.setAttributes(winParams);
        }
    
        class FullscreenHolder extends FrameLayout {
            public FullscreenHolder(Context ctx) {
                super(ctx);
                setBackgroundColor(ContextCompat.getColor(ctx, android.R.color.black));
            }
    
            @Override
            public boolean onTouchEvent(MotionEvent evt) {
                return true;
            }
        }
    
    

    https://github.com/sunandan89/opendatakit.survey/blob/0901aa19a9706f1869ad3f4d8053b117274ab599/src/org/opendatakit/survey/android/views/ODKWebChromeClient.java

    相关文章

      网友评论

          本文标题:video 横屏写法onShowCustomView onHid

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