增强 webView 性能(应该和原生 Web Browser 的性能一样) [英] Enhance webView performance (should be the same performance as native Web Browser)

查看:16
本文介绍了增强 webView 性能(应该和原生 Web Browser 的性能一样)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的经验是,在 WebView 中加载网站比在 Android Web 浏览器中执行相同操作要慢得多.我可以看到所有文件都已加载到我的 Apache 日志中,但是需要几秒钟时间才能在 WebView 控件中显示该页面.在本机 Web 浏览器中打开同一页面将立即显示.渲染似乎有点残缺.

我们必须应用哪些浏览器设置才能获得与在本机 Web 浏览器中加载页面相同的性能?

我们当前的设置:

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

解决方案

我终于知道android webview性能不佳的原因了.注意下图...从 OnPageStarted 到 OnPageFinished 用了 12 秒.因为它应该加载 CSS、javascript 和 ... AJAX ...

我注意到 JQuery 和 JQueryMobile 需要在 Html 中加载所有的 DOM 结构.所以如果我在 OnPageFinished 之后延迟加载 javascript,它应该会更快地显示页面.

首先使用 setTimeout 而不是 $(document).ready(function() {});在 JQuery 中.然后使用lazyload javascript 文件.

最终的 html 和 javascript 是:

<script src="/css/j/lazyload-min.js" type="text/javascript"></script><script type="text/javascript" charset="utf-8">加载完成(){//而不是 $(document).ready(function() {});}函数加载脚本(){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'], 加载完成);}设置超时(加载脚本,10);

你可以在

但它是用中文写的:)

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.

Which browser settings do we have to apply in order to achieve the same performance as loading the page in the native web browser?

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

解决方案

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...

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.

First use setTimeout instead of $(document).ready(function() {}); in JQuery.Then use lazyload javascript file.

The final html and javascript is:

<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

After do that,you can see the log image below:

Now, it only takes 2 seconds from OnPageStarted to OnPageFinished.

I posted the article at https://wenzhang.baidu.com/page/view?key=22fe27eabff3251f-1426227431

But it was written in Chinese:)

这篇关于增强 webView 性能(应该和原生 Web Browser 的性能一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆