admin管理员组文章数量:1193565
I've got a little web app that I made to play with Android's WebView functionality.
I've got some divs that I use as buttons (with onclick
attributes). Upon trying out the app (in the device's browser), I immediately noticed a huge amount of lag after tapping a button. The lag comes between when I tap the button and when the browser shows the orange highlight around it.
I did some testing and got some info:
- JavaScript isn't the problem. I unlinked all my scripts and blanked out all the
onclick
attributes. The performance didn't change. - CSS3 stuff isn't the problem. I got rid of all the fancy gradients, and the performance didn't change.
- The number of elements isn't the problem. I tried it with just a few elements on the page, and performance didn't change.
- Doctype and meta stuff isn't the problem. I made sure I was using what Android recommends.
I'm really at a loss as to why there's so much lag. I've eliminated everything that could be causing it, but nothing's helped.
Am I missing something?
How can I remove lag after a button is tapped?
I've got a little web app that I made to play with Android's WebView functionality.
I've got some divs that I use as buttons (with onclick
attributes). Upon trying out the app (in the device's browser), I immediately noticed a huge amount of lag after tapping a button. The lag comes between when I tap the button and when the browser shows the orange highlight around it.
I did some testing and got some info:
- JavaScript isn't the problem. I unlinked all my scripts and blanked out all the
onclick
attributes. The performance didn't change. - CSS3 stuff isn't the problem. I got rid of all the fancy gradients, and the performance didn't change.
- The number of elements isn't the problem. I tried it with just a few elements on the page, and performance didn't change.
- Doctype and meta stuff isn't the problem. I made sure I was using what Android recommends.
I'm really at a loss as to why there's so much lag. I've eliminated everything that could be causing it, but nothing's helped.
Am I missing something?
How can I remove lag after a button is tapped?
Share Improve this question asked Sep 11, 2011 at 0:34 NathanNathan 7,03212 gold badges40 silver badges56 bronze badges4 Answers
Reset to default 18Basically, click events on mobile browsers are delayed by 300ms. Do you know of the fast button pattern? Basically you can use the touchstart
event (which fires without delay).
Here's a complete explanation: http://code.google.com/mobile/articles/fast_buttons.html
try insert to the html code
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, user-scalable=no">
and quick click click click click is no longer a problem (for me. 4.2.2 and WebChromeClient)
So, WebView holds each tap event to see if it's a double tap or a touch move event. There is an alternative to binding to touchstart events. If you specify with the viewport meta directive that your WebView shouldn't zoom or scroll, touch events will reach your code immediately (since Gingerbread).
You can find the details on the viewport directive here: http://developer.android.com/guide/webapps/targeting.html
As the Google code is very complicated, I implemented my own using Mootools 1.3+:
Element.Events.touch =
{
base: 'touchend',
condition: function(e)
{
return ((new Date().getTime() - this.startT < 200) && this.valid) ? true : false;
},
onAdd: function(fn)
{
this.addEvent('touchstart', function(e)
{
this.valid = true;
this.startT = new Date().getTime();
this.startX = e.touches[0].clientX;
this.startY = e.touches[0].clientY;
});
this.addEvent('touchmove', function(e)
{
if ((Math.abs(e.touches[0].clientX - this.startX) > 10) || (Math.abs(e.touches[0].clientY - this.startY) > 10)) this.valid = false;
});
}
};
Just put this code on your page and now use touch event instead of click:
$('id').addEvent('touch', function()
{
// your code
});
It works by adding a touchend and touchstart event, if they happen in less than 200ms and the touch doesn't get too far it's a valid touch otherwise not.
It worked very well on 2.2 and 4.0
本文标签: javascriptAndroid WebView Very laggy button responseStack Overflow
版权声明:本文标题:javascript - Android WebView: Very laggy button response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738453157a2087606.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论