在网站加载时完成启动画面
Finish splash screen on website loading
我需要在 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);
}
不要为启动画面单独制作 activity,在 MainActivity.java 中创建启动画面布局和 webview 布局,并将 webview 的可见性设置为 GONE。
打开 MainActivity 时初始化 webView 并设置自定义 WebViewClient。在您的自定义 webViewClient 中覆盖 onPageFinished() 并在此方法中使 webview 可见并将初始屏幕布局设置为 Gone。
这里也一样:Loading a WebView URL before splashscreen finishes
我的解决方案在这里
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" />
我需要在 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);
}
不要为启动画面单独制作 activity,在 MainActivity.java 中创建启动画面布局和 webview 布局,并将 webview 的可见性设置为 GONE。
打开 MainActivity 时初始化 webView 并设置自定义 WebViewClient。在您的自定义 webViewClient 中覆盖 onPageFinished() 并在此方法中使 webview 可见并将初始屏幕布局设置为 Gone。
这里也一样:Loading a WebView URL before splashscreen finishes
我的解决方案在这里
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" />