如何在隐藏模式下使用splashscreen启动活动

时间:2013-02-11 09:18:28

标签: android webview screen splash-screen

我有这个应用程序,它开始显示一个3秒的启动画面,然后调用另一个活动,这是一个webview。

CODE SPLASHSCREEN

package test.test;

import test.test.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

public class SplashScreen extends Activity {

int timer = 0;

@Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_splash_screen);

      Thread splashThread = new Thread() {

      @Override
      public void run() {
            try {
               while (timer < 3000) {
                  sleep(100);
                  timer += 100;            
               }
            } catch (InterruptedException e) {
               // do nothing
            } finally {
               finish();
               Intent i = new Intent();
               i.setClassName("test.test","test.test.Mainmenu");
               startActivity(i);
            }
        }
      };
      splashThread.start();
   }
}

CODE XML SPLASH

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:animateLayoutChanges="false"
    android:background="#57a11f" >

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:contentDescription="@string/image"
        android:src="@drawable/splash" />
</RelativeLayout>

CODE WEBVIEW

package test.test;

import test.test.R;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Mainmenu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        setContentView(R.layout.activity_mainmenu);

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setAppCacheMaxSize( 10 * 1024 * 1024 );
        webView.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath()+ "/cache" );
        webView.getSettings().setAllowFileAccess( true );
        webView.getSettings().setAppCacheEnabled( true );
        webView.setBackgroundColor(0x00000000);     
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled( true );
        webView.getSettings().setDomStorageEnabled( true );
        webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );

        if ( !isNetworkAvailable() ) { 
            webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
        }

        webView.loadUrl( "http://www.google.com" );
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }   
}

CODE WEBVIEW XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#57a11f"
    tools:context=".Mainmenu" >

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:animateLayoutChanges="false" 
    />

</RelativeLayout>

问题:当splashscreen调用webview活动时,它会留下一段时间的空白屏幕,下载和呈现页面的时间。

我想说的是:我想知道如何同时启动启动画面和webview,但隐藏webview,以便在后台加载和渲染页面有3秒钟。< / p>

EXTRA:我不是在寻找使用onPageFinished的解决方案,因为我真的希望启动画面保持3秒钟而且使用onPageFinished顺便说一下并不能避免将网页放到屏幕上所需的渲染时间。

谢谢!

1 个答案:

答案 0 :(得分:3)

在同一布局文件中使用ImageView和WebView 最初将webview保持在View.Gone模式中。

一旦spalsh屏幕完成,启用WebView View.Viasible和ImageView View.Gone

Pseudocode :

Webview=View.Gone;
ImageView=View.VISIBLE;
WebView.loadURL="<some url>";
SplasScreen timer for 3 seconds;
WebView=View.VISIBLE;
ImageVIew=View.GONE;

使用RelativeLayout或FrameLayout