开发者

Android WebView performance

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 really bad in webview load url. I have tried everything from using the frame layout to limiting the number of webviews to load. Still the performance is not satisfactory.

How do I optimize the performance of webview? This should be a common usage. Am I missing something obvious.

My Webview settings are - 开发者_如何学C

    webView.setInitialScale(WEBVIEW_SCALE);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(false);
    webView.setWebViewClient(new MyWebViewClient());    
    webView.setOnTouchListener( new OnTouchListener());


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);
}


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


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);
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜