为什么我的Android Webview如此之慢

时间:2015-05-16 15:49:10

标签: android webview

我尝试了一些方法来解决速度问题,但网站在webview上的加载非常慢,即使它在Chrome中打开3秒钟。我尝试的是,在我的代码中添加这两行;

mWebview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
            mWebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

并添加 机器人:硬件加速=“真” 到我的清单文件,因为我在那里找到了这些解决方案。但它不起作用。那有什么不对?谢谢你的建议。

onCreateView我的代码的一部分:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    mPbar = (ProgressBar) rootView.findViewById(R.id.web_view_progress);


    mView = (FrameLayout) rootView.findViewById(R.id.web_view);
    mView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));

    mImageView = new ImageView(getActivity());
    mImageView.setBackgroundColor(getResources().getColor(android.R.color.white));
    mImageView.setImageResource(R.drawable.big_image_loading);
    mImageView.setScaleType(ScaleType.CENTER_INSIDE);
    mImageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    mImageView.post(new Runnable() {
        @Override
        public void run() {
            ((AnimationDrawable) mImageView.getDrawable()).start();
        }
    });
    mView.addView(mImageView);
    mWebview = new WebView(getActivity());
    mWebview.setVisibility(View.GONE);
    mWebview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    if(internetAccess()){
        if (mUrl != null) {

            mWebview.setWebChromeClient(new MyWebChromeClient());
            mWebview.getSettings().setPluginState(PluginState.ON);
            mWebview.getSettings().setUseWideViewPort(true);
            mWebview.getSettings().setDefaultZoom(ZoomDensity.FAR);
            mWebview.getSettings().setBuiltInZoomControls(true);
            mWebview.getSettings().setSupportZoom(true);
            mWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            mWebview.getSettings().setAllowFileAccess(true);
            mWebview.getSettings().setDomStorageEnabled(true);
            mWebview.getSettings().setJavaScriptEnabled(true);
            mWebview.getSettings().setAppCacheEnabled(true);
            mWebview.setWebViewClient(new MyWebViewClient());
            if (Build.VERSION.SDK_INT >= 17)
            mWebview.getSettings().setMediaPlaybackRequiresUserGesture(false);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                mWebview.getSettings().setDisplayZoomControls(false);
            mWebview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
            mWebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            mWebview.loadUrl(mUrl);



        }
        mView.addView(mWebview);
        mBackBtn = (ImageButton) rootView.findViewById(R.id.web_view_btn_back);
        mFowardBtn = (ImageButton) rootView.findViewById(R.id.web_view_btn_forward);
        mRefreshBtn = (ImageButton) rootView.findViewById(R.id.web_view_btn_refresh);
        mRefreshPbar = (ProgressBar) rootView.findViewById(R.id.loading);
        // mShareBtn = (ImageButton) rootView.findViewById(R.id.web_view_btn_share);

        mBackBtn.setOnClickListener(this);
        mFowardBtn.setOnClickListener(this);
        mRefreshBtn.setOnClickListener(this);


        updateActionView();

        return rootView;

    }
}

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题。问题与WebSettings无关。 view.setVisibility(View.VISIBLE);部分是在我的onPageFinished函数上,这就是为什么网站加载速度这么慢。我将它移动到onPageStarted函数,它运行得非常快。