Android WebView performance

后端 未结 4 1235
春和景丽
春和景丽 2020-12-08 05:32

In my app, I am loading a list of external url\'s in webview and allow user to flip through them. Webviews are loaded on to a view flipper. I find the performance is reall

相关标签:
4条回答
  • 2020-12-08 06:02

    Try this:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 
    } else {
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    
    0 讨论(0)
  • 2020-12-08 06:11

    I think the following works best:

    if (Build.VERSION.SDK_INT >= 19) {
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }       
    else {
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    

    Android 19 has Chromium engine for WebView. I guess it works better with hardware acceleration.

    For more info Android 4.4 KitKat, the browser and the Chrome WebView

    0 讨论(0)
  • 2020-12-08 06:12

    Improvise Answer: The above Solution mentioned CacheManager.class is not supported.

     try {
                val m: Method = ServiceWorkerWebSettingsCompat.CacheMode::class.java.getDeclaredMethod(
                    "setCacheDisabled",
                    Boolean::class.javaPrimitiveType
                )
                m.isAccessible = true
                m.invoke(null, true)
            } catch (e: Throwable) {
                Log.i("myapp", "Reflection failed", e)
            }
    
    0 讨论(0)
  • 2020-12-08 06:14

    This has already been discussed here: Enhance webView performance (should be the same performance as native Web Browser)

    I ran into a similar issue, and after some heavy debugging noticed the native browser and WebView browser seem to be using different caches.

    This code can be used to disable the WebView cache, and made WebView much faster for me (though at the expense of not caching). Note that it uses private APIs, so by using it you're risking the code will break in future releases:

    try
    {
      Method m = CacheManager.class.getDeclaredMethod("setCacheDisabled", boolean.class);
      m.setAccessible(true);
      m.invoke(null, true);
    }
    catch (Throwable e)
    {
      Log.i("myapp","Reflection failed", e);
    }
    
    0 讨论(0)
提交回复
热议问题