Android 增强 webView 性能(应该和原生 Web Browser 性能一样)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3652583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enhance webView performance (should be the same performance as native Web Browser)
提问by Hansjoerg
My experience is that loading websites in a WebView is much slower than performing the same actions in the Android Web Browser. I can see that all files have been loaded in my Apache log, it will take a few seconds until the page is displayed in the WebView control, however. Opening the same page in the native Web browser will result in an immediate display. It seems that rendering is somehow crippled.
我的经验是,在 WebView 中加载网站比在 Android Web 浏览器中执行相同的操作要慢得多。我可以看到所有文件都已加载到我的 Apache 日志中,但是需要几秒钟时间才能在 WebView 控件中显示该页面。在本机 Web 浏览器中打开同一页面将立即显示。渲染似乎在某种程度上瘫痪了。
Which browser settings do we have to apply in order to achieve the same performance as loading the page in the native web browser?
我们必须应用哪些浏览器设置才能获得与在本机 Web 浏览器中加载页面相同的性能?
Our current settings:
我们目前的设置:
browserset.setLoadsImagesAutomatically(true);
browserset.setJavaScriptEnabled(true);
browserset.setDatabaseEnabled(true);
browserset.setDatabasePath("data/data/com.xxx/databases");
browserset.setDomStorageEnabled(true);
browserset.setRenderPriority(WebSettings.RenderPriority.HIGH);
browserset.setSupportZoom(false);
browserset.setUserAgentString( browserset.getUserAgentString() + " (XY ClientApp)" );
browserset.setAllowFileAccess(true);
browserset.setSavePassword(false);
browserset.setSupportMultipleWindows(false);
browserset.setAppCacheEnabled(true);
browserset.setAppCachePath("");
browserset.setAppCacheMaxSize(5*1024*1024);
回答by Guypo
I ran into a similar issue, and after some heavy debugging noticed the native browser and WebView browser seem to be using different caches.
我遇到了类似的问题,经过一些繁重的调试后发现本机浏览器和 WebView 浏览器似乎使用了不同的缓存。
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:
此代码可用于禁用 WebView 缓存,并使 WebView 对我来说更快(尽管以不缓存为代价)。请注意,它使用私有 API,因此通过使用它,您可能会冒着代码在未来版本中损坏的风险:
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);
}
回答by DennisZhong
I finally got the reason of android webview bad performance issue. Notice the image below... It used 12 seconds from OnPageStarted to OnPageFinished. Because it should load CSS,javascript and ... AJAX...
我终于得到了 android webview 性能问题的原因。注意下图...从 OnPageStarted 到 OnPageFinished 用了 12 秒。因为它应该加载 CSS、javascript 和 ... AJAX ...
I notice that JQuery and JQueryMobile need load all DOM struct in Html.So if I lazy load the javascript after OnPageFinished,it should show page faster.
我注意到 JQuery 和 JQueryMobile 需要加载 Html 中的所有 DOM 结构。所以如果我在 OnPageFinished 之后延迟加载 javascript,它应该显示页面更快。
First use setTimeout instead of $(document).ready(function() {}); in JQuery.Then use lazyload javascript file.
首先使用 setTimeout 而不是 $(document).ready(function() {}); 在 JQuery 中。然后使用lazyload javascript 文件。
The final html and javascript is:
最终的 html 和 javascript 是:
<script src="/css/j/lazyload-min.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
loadComplete(){
//instead of $(document).ready(function() {});
}
function loadscript()
{
LazyLoad.loadOnce([
'/css/j/jquery-1.6.2.min.js',
'/css/j/flow/jquery.flow.1.1.min.js',
'/css/j/min.js?v=2011100852'
], loadComplete);
}
setTimeout(loadscript,10);
</script>
You can find lazyload-min.js in http://wonko.com/post/painless_javascript_lazy_loading_with_lazyload
您可以在http://wonko.com/post/painless_javascript_lazy_loading_with_lazyload 中找到lazyload-min.js
After do that,you can see the log image below:
完成后,您可以看到下面的日志图像:
Now, it only takes 2 seconds from OnPageStarted to OnPageFinished.
现在,从 OnPageStarted 到 OnPageFinished 只需要 2 秒钟。
I posted the article at https://wenzhang.baidu.com/page/view?key=22fe27eabff3251f-1426227431
我把文章发在https://wenzhang.baidu.com/page/view?key=22fe27eabff3251f-1426227431
But it was written in Chinese:)
但它是用中文写的:)