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

video 横屏写法onShowCustomView onHid

作者: cain07 | 来源:发表于2022-03-14 22:08 被阅读0次

    https://github.com/tuisme/CO3038_2018/blob/8a72a70211b4c27dfab643513c0fdc1bed9fad8c/CO3038/Tools/Browser/app/src/main/java/de/baumann/browser/Activity/BrowserActivity.java#L2757

    @Override
        public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
            if (view == null) {
                return;
            }
            if (customView != null && callback != null) {
                callback.onCustomViewHidden();
                return;
            }
    
            customView = view;
            originalOrientation = getRequestedOrientation();
    
            fullscreenHolder = new FullscreenHolder(this);
            fullscreenHolder.addView(
                    customView,
                    new FrameLayout.LayoutParams(
                            FrameLayout.LayoutParams.MATCH_PARENT,
                            FrameLayout.LayoutParams.MATCH_PARENT
                    ));
    
            FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
            decorView.addView(
                    fullscreenHolder,
                    new FrameLayout.LayoutParams(
                            FrameLayout.LayoutParams.MATCH_PARENT,
                            FrameLayout.LayoutParams.MATCH_PARENT
                    ));
    
            customView.setKeepScreenOn(true);
            ((View) currentAlbumController).setVisibility(View.GONE);
            setCustomFullscreen(true);
    
            if (view instanceof FrameLayout) {
                if (((FrameLayout) view).getFocusedChild() instanceof VideoView) {
                    videoView = (VideoView) ((FrameLayout) view).getFocusedChild();
                    videoView.setOnErrorListener(new VideoCompletionListener());
                    videoView.setOnCompletionListener(new VideoCompletionListener());
                }
            }
            customViewCallback = callback;
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    
    
        public boolean onHideCustomView() {
            if (customView == null || customViewCallback == null || currentAlbumController == null) {
                return false;
            }
    
            FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
            if (decorView != null) {
                decorView.removeView(fullscreenHolder);
            }
    
            customView.setKeepScreenOn(false);
            ((View) currentAlbumController).setVisibility(View.VISIBLE);
            setCustomFullscreen(false);
    
            fullscreenHolder = null;
            customView = null;
            if (videoView != null) {
                videoView.setOnErrorListener(null);
                videoView.setOnCompletionListener(null);
                videoView = null;
            }
            setRequestedOrientation(originalOrientation);
            ninjaWebView.reload();
    
            return true;
        }
    
    
    //用于和video标签 全屏按钮的 监听  点击全屏按钮
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            Log.e("Media", "onShowCustomView ............ ");
           if (makePhotoUtils!=null){
               makePhotoUtils.setFullScreen();
           }
            if (myCallback != null) {
                myCallback.onCustomViewHidden();
                myCallback = null;
                Log.e("Media", "myCallback.onCustomViewHidden()...");
                return;
            }
            long id = Thread.currentThread().getId();
            if (makePhotoUtils==null) return;
            ViewGroup parent = (ViewGroup) makePhotoUtils.webView.getParent();
            String s = parent.getClass().getName();
            Log.v("WidgetChromeClient", "rong debug Ex: " + s);
            parent.removeView(makePhotoUtils.webView);
            parent.addView(view);
            myView = view;
            myCallback = callback;
        }
        private View myView = null;
        private CustomViewCallback myCallback = null;
        //用于和video标签 全屏按钮的 监听  点击关闭全屏按钮
        @Override
        public void onHideCustomView() {
            long id = Thread.currentThread().getId();
            Log.v("Media", "onrong debug in hideCustom Ex: " + id);
            if (makePhotoUtils!=null){
                makePhotoUtils.setNoFullScreen();
            }
            if (myView != null) {
     
                if (myCallback != null) {
                    myCallback.onCustomViewHidden();
                    myCallback = null;
                }
     
                ViewGroup parent = (ViewGroup) myView.getParent();
                parent.removeView(myView);
                parent.addView(makePhotoUtils.webView);
                myView = null;
            }
    
    

    2

        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);
        }
    
    public class WindowManagerUtils {
    
      public static void fullScreen(Activity pActivity) {
        WindowManager.LayoutParams attrs = pActivity.getWindow().getAttributes();
        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        pActivity.getWindow().setAttributes(attrs);
        if (android.os.Build.VERSION.SDK_INT >= 14) {
          //noinspection all
          pActivity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        }
      }
    
      public static void smallScreen(Activity pActivity) {
        WindowManager.LayoutParams attrs = pActivity.getWindow().getAttributes();
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        pActivity.getWindow().setAttributes(attrs);
        if (android.os.Build.VERSION.SDK_INT >= 14) {
          //noinspection all
          pActivity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        }
      }
    }
    
    // 智能开启硬件加速
      public static void refreshHardwareAccelerationSetting(WebView _webView) {
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
            && _webView.getHeight() < 4096
            && _webView.getWidth() < 4096) {
          _webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else {
          _webView.setLayerType(View.LAYER_TYPE_NONE, null);
        }
      }
    
      // 关闭硬件加速
      public static void closeHardwareAcceleration(WebView _webView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
          _webView.setLayerType(View.LAYER_TYPE_NONE, null);
        }
      }
    

    https://www.tabnine.com/code/java/methods/android.webkit.WebChromeClient$CustomViewCallback/onCustomViewHidden

    相关文章

      网友评论

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

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