[英]Android Webview : Splash screen while loading a url
我是Android應用程序的新手,這是我的第一個應用程序。 我已經創建了初始屏幕並可以運行..但是消失之后,會有一個長長的白色空白屏幕,大約2-5秒,然后該網址開始加載。
我的問題是..如何在加載URL時在啟動畫面中添加一些加載或進度欄? 因此不再有白色的空白屏幕。 對不起,我的英語不好。
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.xxx" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="xxx"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true" />
<activity
android:name="com.xxx.xxx.Splash"
android:label="xxx"
android:theme="@style/Theme.MyAppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyActivity"
android:hardwareAccelerated="true"
android:configChanges="orientation|screenSize"
android:label="xxx" >
</activity>
</application>
</manifest>
activity_my.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MyActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<FrameLayout
android:id="@+id/customViewContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"/>
</LinearLayout>
Splash.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
final Handler handel = new Handler();
handel.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent loadSplash = new Intent(Splash.this, MyActivity.class);
startActivity(loadSplash);
finish();
}
}, 3000);
}
}
splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/splash" />
</RelativeLayout>
不要將啟動屏幕創建為單獨的活動。 在您的WebView
活動(即頁面加載活動)上添加啟動功能。 偵聽onPageFinished
事件,並隱藏初始圖像或動畫。
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
//hide splash functionality
}
});
如果您想顯示進度條,還可以通過它獲得頁面加載進度
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
Pbar.setVisibility(ProgressBar.VISIBLE);
}
Pbar.setProgress(progress);
if(progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
}
}
});
嘗試這兩個選項之一。希望它將對您有幫助
1.刪除處理程序的回叫
handel.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent loadSplash = new Intent(Splash.this, MyActivity.class);
startActivity(loadSplash);
finish();
handel.removeCallbacks(this);
}
}, 3000);
使用線程代替處理程序
public class Splash extends Activity { protected boolean _active = true; protected int _splashTime = 3000; Thread splashTread; private boolean stop = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splashscreen); splashTread = new Thread() { @Override public void run() { try { int waited = 0; while (_active && (waited < _splashTime)) { sleep(100); if (_active) { waited += 100; } } } catch (InterruptedException e) { // do nothing } finally { if (!stop) { startActivity(new Intent(Splash.this, MyActivity.class)); finish(); } else finish(); } } }; splashTread.start(); }
}
這是怎么做-
wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView1).setVisibility(View.VISIBLE);
}
});
wv.loadUrl("http://yoururlhere.com");
您可以在此處找到完整的示例- 加載URL時的啟動屏幕
就在這里..正如吉布蘭·汗所說,不要創建單獨的啟動活動。 在Webview上使用啟動功能。 首先,通過將其擴展為WebViewClient
創建一個webclient類,您將獲得像這樣的未實現方法。
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onLoadResource(WebView view, String url) {
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar1.setVisibility(View.VISIBLE);
webview.setVisibility(View.GONE);
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
progressBar1.setVisibility(View.GONE);
webview.setVisibility(View.VISIBLE);
}
}
`按照上面的代碼,使用進度條作為加載指示符。
不要忘記這樣做web.setWebViewClient(new MyWebViewClient());
其中MyWebViewClient
是您的webclient類名稱
這是上面代碼的xml
<WebView
android:id="@+id/web"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.