2015-05-16 93 views
2

我嘗試了一些方法來解決速度問題,但是該網站在web瀏覽器中的加載速度非常緩慢,即使它在Chrome中以3秒爲單位打開。我嘗試的是,在我的代碼中添加這兩行代碼;爲什麼我的Android Webview速度如此之慢

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; 

    } 
} 

回答

0

OK,我解決了這個問題。問題不在於WebSettings。 view.setVisibility(View.VISIBLE);部分是在我的onPageFinished函數,這就是爲什麼網站加載如此緩慢。我將它移動到onPageStarted函數上,它的工作速度非常快。

相關問題