2011-12-05 34 views
5

Tôi đang tạo một ứng dụng có nội dung Webview trực tuyến với các phần tử khác, được lồng trong một ScrollView. Tôi đã nhận thấy rằng trong ICS, thử nghiệm trên thiết bị Galaxy Nexus, WebView dường như bị tách rời khỏi phần còn lại của các phần tử được hiển thị khi trang được đưa vào, khiến cho WebView xuất hiện trôi nổi vì giá trị của một vài ms lag trên bản vẽ. Điều này không xảy ra trong phiên bản 2.x của Android (không được thử nghiệm trên 3.x). Đây là video có hiệu ứng nổi http://www.youtube.com/watch?v=avfBoWmooM4 (bạn có thể thấy rõ ràng nếu bạn đặt ở chế độ toàn màn hình ở 1080p)Bản vẽ WebView rời rạc trong ICS

Có ai có thể đề xuất tại sao điều này có thể xảy ra hoặc khắc phục không?

tôi đã thiết lập một dự án thử nghiệm, dưới đây, để chứng minh:

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; 
     } 

    } 
} 

Trả lời

1

Tôi không biết làm thế nào để sửa chữa nó nhưng bây giờ tôi lý do. Nội dung của WebView được hiển thị bởi android.webkit.WebViewCore trong chuỗi công việc riêng biệt của nó. Họ đang giao tiếp với nhau. Khi WebView cần tái render nó sẽ gửi một thông báo "please render" tới WebViewCore và khi WVC đã sẵn sàng thì gửi lại kết quả. Vấn đề là việc hiển thị của họ không đồng bộ với việc hiển thị các phần tử giao diện người dùng khác - vì nó được thực hiện trên một chuỗi riêng biệt, không phải là giao diện người dùng.

Tôi đoán họ muốn tránh chặn luồng ui bằng tất cả nỗ lực hiển thị. Điều đó rất tử tế. Nhưng nó gây ra các vấn đề khác ... như của bạn.

Các vấn đề liên quan