ICS中脱节的WebView绘图

Mar*_*tyn 5 android android-webview android-4.0-ice-cream-sandwich

我正在创建一个具有Webview内联其他元素的应用程序,嵌套在ScrollView中.我注意到在ICS中,在Galaxy Nexus设备上进行测试时,当页面被抛出时,WebView似乎与其余显示的元素脱节,导致WebView看起来浮动,因为它有几毫秒的值落后于绘画.这不会发生在Android的2.x版本中(未在3.x上测试).这是一个浮动效果的视频http://www.youtube.com/watch?v=avfBoWmooM4(如果你设置为1080p全屏,你可以看清楚)

任何人都可以建议为什么会发生这种情况或修复?

我在下面设置了一个测试项目来演示:

package com.martynhaigh.floating_web_view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class FloatingWebViewTestActivity extends FragmentActivity {
    public final int viewId = 0x12345678;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        FrameLayout frame = new FrameLayout(this);
        frame.setId(viewId);
        setContentView(frame, lp);

        FloatingWebViewICSFragment fragment = new FloatingWebViewICSFragment();
        getSupportFragmentManager().beginTransaction().add(viewId, fragment).commit();
    }

    public static class FloatingWebViewICSFragment extends Fragment {

        private final String htmlBody = "<html><body><p style=\"font-size:1.5em\">This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. </body></html>";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            TextView textView = new TextView(getActivity().getApplicationContext());
            textView.setText("Test Activity");

            WebView webView = new WebView(getActivity().getApplicationContext());
            webView.loadDataWithBaseURL("file:///android_asset/", htmlBody, "text/html", "UTF-8", "about:blank");
            webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            webView.setScrollContainer(false);

            TextView textView2 = new TextView(getActivity().getApplicationContext());
            textView2.setText("Test Activity");

            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

            LinearLayout layout = new LinearLayout(getActivity().getApplicationContext());
            layout.setLayoutParams(lp);
            layout.setOrientation(LinearLayout.VERTICAL);

            layout.addView(textView);
            layout.addView(webView);
            layout.addView(textView2);

            ScrollView scrollView = new ScrollView(getActivity().getApplicationContext());
            scrollView.setLayoutParams(lp);
            scrollView.addView(layout);

            return scrollView;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Zso*_*any 1

我不知道如何解决它,但我现在知道原因了。WebView 的内容由 android.webkit.WebViewCore 在其单独的工作线程中呈现。他们正在互相交流。当 WebView 需要重新渲染时,它会向 WebViewCore 发送一条“请渲染”消息,当 WVC 准备就绪时,它会发回结果。关键是它们的渲染与其他 UI 元素的渲染不同步 - 因为它是在单独的非 UI 线程上完成的。

我猜他们想避免所有渲染工作都阻塞 ui 线程。这真是太好了。但它会导致其他问题......就像你的问题一样。