2017-02-12 94 views
0

我需要顯示啓動畫面,直到網頁加載到webview。完成網站加載啓動畫面

我使用下面的代碼。有可能嗎?

public class SplashScreen extends Activity { 

    protected Intent intent; 

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

     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     setContentView(R.layout.activity_splash_screen); 

     intent = new Intent(getApplicationContext(), MainActivity.class); 

     // MainActivity.class contains WebView 

     Thread myThread = new Thread() { 
      @Override 
      public void run() { 

       try { 
        sleep(5000); 
        startActivity(intent); 
        finish(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 

     }; 

     myThread.start(); 
    } 
} 

而且

public class MainActivity extends Activity { 

    private WebView view; 

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

     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     setContentView(R.layout.activity_main); 

     String url = "http://google.com"; 
     view = (WebView)this.findViewById(R.id.webView1); 

     view.clearCache(true); 
     WebSettings s = view.getSettings(); 
     s.setJavaScriptEnabled(true); 

     s.setCacheMode(WebSettings.LOAD_DEFAULT); 
     s.setDomStorageEnabled(true); 

     view.loadUrl(url); 
    } 

回答

1

不要爲閃屏的單獨活動,在MainActivity.java本身創建一個啓動畫面佈局和網頁流量的佈局和web視圖來GONE設置可見。

打開MainActivity後,初始化webView並設置自定義的WebViewClient。在自定義webViewClient中重寫onPageFinished(),並使用此方法使webview可見並啓動屏幕布局。

同樣在這裏:Loading a WebView URL before splashscreen finishes

0

我的解決辦法是在這裏

private WebView view; 
private ImageView splashScreen; 

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

    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    setContentView(R.layout.activity_main); 

    splashScreen = (ImageView) this.findViewById(R.id.spscreen); 

    String url = "http://google.com"; 
    view = (WebView)this.findViewById(R.id.webView1); 

    view.setWebViewClient(new WebViewClient() {    

     public void onPageFinished(WebView view, String url) { 
      // do your stuff here 
      splashScreen.setVisibility(View.INVISIBLE); 
      view.setVisibility(View.VISIBLE); 
     } 
    }); 

     WebSettings s = view.getSettings(); 
    s.setJavaScriptEnabled(true); 

    s.setCacheMode(WebSettings.LOAD_DEFAULT); 
    s.setDomStorageEnabled(true); 

    view.loadUrl(url); 
} 

和佈局

<ImageView 
    android:id="@+id/spscreen" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:src="@drawable/splashscreen" 
    android:visibility="visible" 
    android:scaleType="fitXY"/> 

<WebView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/webView1" 
    android:visibility="invisible" />