Reputation: 11
i'm loading a website into an android webview and i would like to improve scrolling performance. In other words i would like to make scrolling faster and smoother for the user. Any ideas? Thanks in advance
Upvotes: 0
Views: 1520
Reputation: 11
I also encountered sluggish scrolling with erratic fling scrolling when embedding WebView in a fragment inside a HorizontalScrollView, but it seems to happen in other cases also. After days of trial and error, here is a solution that seems to work. The critical part is the OnTouchListener; I don't believe that the WebView settings are that important. It is counterintuitive, but you have to explicitly tell the WebView to scroll... seems like an Android bug.
WebView myWebView;
VelocityTracker mVelocityTracker;
myWebView.setOnTouchListener(new WebViewOnTouchListener());
private class WebViewOnTouchListener implements View.OnTouchListener {
float currY = 0;
float yVeloc = 0f;
WebView localWebView = myWebView;
@Override
public boolean onTouch(View v, MotionEvent event) {
int index = event.getActionIndex();
int action = event.getActionMasked();
int pointerId = event.getPointerId(index);
switch(action){
case MotionEvent.ACTION_DOWN:
if (mVelocityTracker == null) {
// Retrieve a new VelocityTracker object to watch the velocity
// of a motion.
mVelocityTracker = VelocityTracker.obtain();
} else {
// Reset the velocity tracker back to its initial state.
mVelocityTracker.clear();
}
mVelocityTracker.addMovement(event);
currY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity(1000);
yVeloc = mVelocityTracker.getYVelocity(pointerId);
float y = event.getY();
localWebView.scrollBy(0, (int) (currY - y));
currY = y;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
localWebView.flingScroll(0, -(int)yVeloc);
break;
}
return true;
}
}
Upvotes: 1
Reputation: 535
did you declare that you want software rendering in your application's manifest (or by setting the WebView's layer type)? Maybe try hardware mode
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Upvotes: 2