【发布时间】:2018-08-22 22:20:20
【问题描述】:
我正在将网站加载到 android webview 中,我想提高滚动性能。换句话说,我想让用户的滚动更快更流畅。有任何想法吗? 提前致谢
【问题讨论】:
-
可以分享网站链接吗?
-
这是不可能的我很抱歉:(
标签: android performance webview scroll
我正在将网站加载到 android webview 中,我想提高滚动性能。换句话说,我想让用户的滚动更快更流畅。有任何想法吗? 提前致谢
【问题讨论】:
标签: android performance webview scroll
您是否声明要在应用程序的清单中进行软件渲染(或通过设置 WebView 的图层类型)?试试硬件模式
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
【讨论】:
在将 WebView 嵌入到 HorizontalScrollView 内的片段中时,我也遇到过缓慢滚动和不稳定的滚动,但它似乎也发生在其他情况下。经过几天的反复试验,这里有一个似乎可行的解决方案。关键部分是 OnTouchListener;我不相信 WebView 设置有那么重要。这是违反直觉的,但你必须明确告诉 WebView 滚动......这似乎是一个 Android 错误。
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;
}
}
【讨论】: