Android-WebView中全屏问题

问题详述:

使用WebView加载SaaS-H5观看页时,点击全屏按钮无反应

解决方案:

  1. WebSettings配置需要添加webSettings.setUseWideViewPort(true);

  2. 处理自定义WebChromeClient

    public class WVChromeClient extends WebChromeClient {
        private Context _context;
        private AndroidWebViewActivity _m;
        public WVChromeClient(Context context, AndroidWebViewActivity mainActivity)
        {
            _context = context;
            _m = mainActivity;
        }
    
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            Log.d("MainActivity","newProgress:"+ newProgress );
        }
    
        @Nullable
        @Override
        public Bitmap getDefaultVideoPoster() {
            return Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
        }
    
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            _m.showCustomView(view ,callback);
        }
    
        @Override
        public void onHideCustomView() {
            _m.hideCustomView();
        }
    }
  3. 处理全屏,退出全屏

    /** 视频全屏参数 */
        protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        private View customView;
        private FrameLayout fullscreenContainer;
        private WebChromeClient.CustomViewCallback customViewCallback;
        /** 视频播放全屏 **/
        public void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
            // if a view already exists then immediately terminate the new one
            if (customView != null) {
                callback.onCustomViewHidden();
                return;
            }
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            AndroidWebViewActivity.this.getWindow().getDecorView();
    
            FrameLayout decor = (FrameLayout) getWindow().getDecorView();
            fullscreenContainer = new FullscreenHolder(AndroidWebViewActivity.this);
            fullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
            decor.addView(fullscreenContainer, COVER_SCREEN_PARAMS);
            customView = view;
            setStatusBarVisibility(false);
            customViewCallback = callback;
        }
    
        /** 隐藏视频全屏 */
        public void hideCustomView() {
            if (customView == null) {
                return;
            }
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setStatusBarVisibility(true);
            FrameLayout decor = (FrameLayout) getWindow().getDecorView();
            decor.removeView(fullscreenContainer);
            fullscreenContainer = null;
            customView = null;
            customViewCallback.onCustomViewHidden();
            webView.setVisibility(View.VISIBLE);
        }
    
        /** 全屏容器界面 */
        static class FullscreenHolder extends FrameLayout {
    
            public FullscreenHolder(Context ctx) {
                super(ctx);
                setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
            }
    
            @Override
            public boolean onTouchEvent(MotionEvent evt) {
                return true;
            }
        }
    
        private void setStatusBarVisibility(boolean visible) {
            int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    
        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    /** 回退键 事件处理 优先级:视频播放全屏-网页回退-关闭页面 */
                    if (customView != null) {
                        hideCustomView();
                    } else if (webView.canGoBack()) {
                        webView.goBack();
                    } else {
                        finish();
                    }
                    return true;
                default:
                    return super.onKeyUp(keyCode, event);
            }
        }
  4. Manifest中配置activity属性

    <activity android:name=".AndroidWebViewActivity"
                android:configChanges="keyboardHidden|orientation|screenSize"/>

Last updated