【问题标题】:How to improve scrolling in android webview?如何改善android webview中的滚动?
【发布时间】:2018-08-22 22:20:20
【问题描述】:

我正在将网站加载到 android webview 中,我想提高滚动性能。换句话说,我想让用户的滚动更快更流畅。有任何想法吗? 提前致谢

【问题讨论】:

  • 可以分享网站链接吗?
  • 这是不可能的我很抱歉:(

标签: android performance webview scroll


【解决方案1】:

您是否声明要在应用程序的清单中进行软件渲染(或通过设置 WebView 的图层类型)?试试硬件模式

webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

【讨论】:

  • 我试过这个,但它并没有显着提高我的性能:/
  • 可能页面中的js和css很多?
  • 确实有很多 js 和 css 但桌面版运行良好,我的意思是如果你打开浏览器访问此页面,它运行良好
【解决方案2】:

在将 WebView 嵌入到 Horizo​​ntalScrollView 内的片段中时,我也遇到过缓慢滚动和不稳定的滚动,但它似乎也发生在其他情况下。经过几天的反复试验,这里有一个似乎可行的解决方案。关键部分是 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;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多