Search in sources :

Example 1 with CommonWebView

use of cn.foxconn.matthew.myapp.wanandroid.view.CommonWebView in project MyApp by MatthewDevelop.

the class WebViewPresenter method setWebView.

public void setWebView(WebView webView, String url) {
    final CommonWebView commonWebView = getView();
    final ProgressBar progressBar = getView().getProgressBar();
    // TODO WebView的设置有待学习
    WebSettings webSettings = webView.getSettings();
    // 设置JavaScript可用
    webSettings.setJavaScriptEnabled(true);
    // 设置屏幕适应
    // 设置支持缩放,是下项设置的前提
    webSettings.setSupportZoom(true);
    // 设置可以缩放缩放
    webSettings.setBuiltInZoomControls(true);
    // 设置隐藏原生缩放控件
    webSettings.setDisplayZoomControls(false);
    // 设置调整图片至合适webView大小
    webSettings.setUseWideViewPort(true);
    // 设置缩放至屏幕大小
    webSettings.setLoadWithOverviewMode(true);
    // 支持通过js打开新的窗口
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    // 设置默认编码格式
    webSettings.setDefaultTextEncodingName("utf-8");
    // 设置自动加载图片
    webSettings.setLoadsImagesAutomatically(true);
    // 设置支持内容重新布局
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    // 开启Application Cache(html5缓存)功能
    webSettings.setAppCacheEnabled(true);
    // 开启Dom Storage功能
    webSettings.setDomStorageEnabled(true);
    webView.setWebViewClient(new WebViewClient() {

        String startUrl;

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);
            startUrl = url;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
        }

        /**
         * 跳转链接在此方法中执行
         * @param view
         * @param url
         * @return
         */
        // TODO shouldOverrideUrlLoading(WebView view, String url)和 shouldOverrideUrlLoading(WebView view, WebResourceRequest request)区别
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (startUrl != null && url.equals(startUrl)) {
                view.loadUrl(url);
            } else {
                return super.shouldOverrideUrlLoading(view, url);
            }
            return true;
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            progressBar.setProgress(newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            commonWebView.setTitle(title);
        }
    });
    webView.loadUrl(url);
}
Also used : Bitmap(android.graphics.Bitmap) WebSettings(android.webkit.WebSettings) CommonWebView(cn.foxconn.matthew.myapp.wanandroid.view.CommonWebView) WebChromeClient(android.webkit.WebChromeClient) WebView(android.webkit.WebView) CommonWebView(cn.foxconn.matthew.myapp.wanandroid.view.CommonWebView) ProgressBar(android.widget.ProgressBar) WebViewClient(android.webkit.WebViewClient)

Aggregations

Bitmap (android.graphics.Bitmap)1 WebChromeClient (android.webkit.WebChromeClient)1 WebSettings (android.webkit.WebSettings)1 WebView (android.webkit.WebView)1 WebViewClient (android.webkit.WebViewClient)1 ProgressBar (android.widget.ProgressBar)1 CommonWebView (cn.foxconn.matthew.myapp.wanandroid.view.CommonWebView)1