Is there a way to improve the speed of loading an local .html
file into a WebView
. The .html
files are stored in the /assets
Folder.
I uploaded a video with the behaviour here.
As you can see in the video, TextView
(red beackground) is rendered before the transistion starts, while the text in the WebView
is shown afterwards. How can i achieve to load the WebView
as fast as the TextView?
//current implementation webView.setInitialScale(1); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setLoadWithOverviewMode(true); webView.getSettings().setUseWideViewPort(true); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setScrollbarFadingEnabled(false); webView.loadUrl("file:///android_asset/disclaimer.html");
3 Answers
Answers 1
WebView will always have longer render times than a native TextView. I think it's nature of platform but maybe you can try reading html content to a String before loading screen. And then set String with:
webview.loadDataWithBaseURL("", earlyReadedHtmlString, "text/html", "UTF-8", "");
In theoretically, this must be faster. Another way is wait for onPageFinished() and then show textview. It can be a workaround.
Answers 2
It depends on the web application being loaded. Try some of the approaches below:
Set higher render priority (deprecated from API 18+):
webview.getSettings().setRenderPriority(RenderPriority.HIGH);
Enable/disable hardware acceleration:
if (Build.VERSION.SDK_INT >= 19) { // chromium, enable hardware acceleration webView.setLayerType(View.LAYER_TYPE_HARDWARE, null); } else { // older android version, disable hardware acceleration webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); }
Disable the cache:
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Answers 3
A webview cannot ever be rendered as fast as a textview (unless you really are abusing the textview such as loading thousands of lines of text) no matter how many switches you turn on/off and how many optimizations and tweaks you try. The rendering mechanism favours a textview because it is more limited in how you can specify changes to its normal presentation, because a webview can load external ressources, because css modify presentation, because javascript can influence presentation, because it needs more memory and initializations to support all these functions, ...
It is the reason why a phonegap/cordova app cannot ever be as snappy and interactive as a native app. Well, unless the native app is only made of webviews :P
You still can try to improve webview's load/render time by modifying it's configuration but you are better to wait until your content is defined. All those settings, allow improvements under specific circumstances and, for example, changing the layer type may help or worsen your render time for different pages. As Nirmal mentionned in his answer, some of those known to have a bigger impact are layerType, cacheMode and renderPriority
I assume tough, that what you wanted to achieve is prevent your users to see an incomplete screen. You could render a webview before showing your screen by using onPageFinished but it would not load faster, the user would have to wait longer until the screen transitions and you'd probably need a spinning wheel.
In my experience, the way you implemented your transition is the better user experience and I would wait until you have your html content defined before trying to optimize load times.
If you already find the current experience already irritating tough, I would consider taking all html content / webviews out.
0 comments:
Post a Comment