Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve performance of JS draggable menu in WebView on Android

We have a web application shown in WebView on Android and we have performance issue on older Android tablets that we really need to deal with

  • We are using iScroll-lite for javascript dragging
  • We set hardware acceleration to true, set rendering priority to high and the WebView lets through only one MOVE touch event in 80ms interval

Is there anything more we can do?
Is there maybe some faster alternative to iScroll-lite?

We don't know what exactly makes it so slow. It for example runs on phone Sony Erricson with 1 GHz, 512 MB RAM with Android 2.3 smoothly but on tablet Qualcomm 1GHz, 512 RAM with Android 4.0 you make a drag and you actually have to wait to see any outcome. The only difference I see in Android version and screen resolution.

like image 781
Ragnar Avatar asked Dec 29 '12 17:12

Ragnar


People also ask

How do I enable JavaScript on WebView?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

Can we save login data in Android WebView?

Saving passwords in WebView will not be supported in future versions. Retrieves HTTP authentication credentials for a given host and realm from the WebViewDatabase instance.

What is alternative of WebView in android?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.


3 Answers

It might not be the Javascript after all. Android WebViews tend to have quite unexpected performance issues.

Try to deactivate all unnecessary CSS and see if performance improves. If so, you can re-add the CSS styles iteratively to locate the culprit.

And do not use any kind of animated GIFs. Our experience is that this (strangely enough) drags down performance drastically.

like image 87
sebwebdev Avatar answered Sep 28 '22 13:09

sebwebdev


My suggestion is that you write the drag by yourself. It's not difficult. The key part of code looks like below:

var yourDomElement = document.getElementById("demoDrag");
yourDomElement.addEventListener("touchstart", function (e) {
    e.preventDefault();
    //todo
});
yourDomElement.addEventListener("touchmove", function (e) {
    //todo: 
});
yourDomElement.addEventListener("touchend", function (e) {
    //todo
});

You can find many sample code here (e.g. /assets/www/scripts/novas/framwork/nova.Carousel.js, also the scroller.js). You should read the sample code to learn how to write your own "drag".

If you still have no idea, you can contact our team to finish your task for you. We make phonegap apps with very good performance.

like image 34
Leo Cai Avatar answered Sep 28 '22 11:09

Leo Cai


What about using div with overflow set to auto and then applying this fix so that elements can be scrolled

http://chris-barr.com/index.php/entry/scrolling_a_overflowauto_element_on_a_touch_screen_device/

Short extract:

function touchScroll(id){
    var el=document.getElementById(id);
    var scrollStartPos=0;

    document.getElementById(id).addEventListener("touchstart", function(event) {
        scrollStartPos=this.scrollTop+event.touches[0].pageY;
        event.preventDefault();
    },false);

    document.getElementById(id).addEventListener("touchmove", function(event) {
        this.scrollTop=scrollStartPos-event.touches[0].pageY;
        event.preventDefault();
    },false);
}
like image 24
Swav Avatar answered Sep 28 '22 13:09

Swav