Android:如何在webview完成加载网址之前制作启动画面?

时间:2013-11-19 12:30:34

标签: java android xml webview

如果在webview加载网址之前我如何制作启动画面?

我的档案

MainActivity.java SplashActivity.java

splash.xml activity_main.xml中

代码MainActivity.java

public class MainActivity extends Activity {
    WebView website;
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        getWindow().requestFeature(Window.FEATURE_PROGRESS);
                setContentView(R.layout.activity_main);
        final Activity activity = this;

        website = (WebView)findViewById(R.id.webView1);
        website.getSettings().setJavaScriptEnabled(true);
        website.setWebViewClient(new WebViewClient()
                {   
             @Override
             public boolean shouldOverrideUrlLoading(WebView view, String url) {
             return false;
             }  
        });
        website.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) 
               {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
                if(progress == 100)
                  activity.setTitle(website.getTitle());
                WebView browser = (WebView) findViewById(R.id.webView1);
                browser.getSettings().setLoadWithOverviewMode(true);
                browser.getSettings().setUseWideViewPort(true); 
                }
        });


        website.loadUrl("http://*********.com/");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Code SplashActivity.java

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.jehad4t.stikana.R;

public class SplashActivity extends Activity {
    private long splashDelay = 5000; //5 seconds

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        TimerTask task = new TimerTask()
        {

            @Override
            public void run() {
                finish();
                Intent mainIntent = new Intent().setClass(SplashActivity.this, MainActivity.class);
                startActivity(mainIntent);
            }

        };

        Timer timer = new Timer();
        timer.schedule(task, splashDelay);
    }
}

代码splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/splash" />

</LinearLayout>

代码activity_main.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"
    tools:context=".MainActivity" 
    >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:visibility="gone"
       />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

在SplashActivity中更改:

private long splashDelay = 5000;private long splashDelay = 0;

       @Override
        public void run() {
            finish();
            Intent mainIntent = new Intent().setClass(SplashActivity.this, MainActivity.class);
            overridePendingTransition(0, 0);
            startActivity(mainIntent);
        }

在MainActivity XML文件中:添加一个linearlayout。 在MainActivity java文件中:将您的启动图像提供给您在splash.java文件中提供的此线性布局。 像mLayout.setBackgroundResource(R.drawable.splash);

这适合我。

答案 1 :(得分:0)

尝试使用WebViewClient,它有一个onPageFinished方法。我的代码示例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.whatever);

    //make your splash visible here

    WebView browser = (WebView) findViewById(R.id.yourWebView);
    //remove flicker
    browser.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    browser.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            //make it invisible here
        }
    });

    browser.loadUrl("file:///android_asset/whatever.html");
}