Android开发基础之创建启动界面Splash Screen的方法

2019-12-10 19:57:57刘景俊

易采站长站为您分析Android开发基础之创建启动界面Splash Screen的方法,以实例形式较为详细的分析了Android定制启动界面的布局及功能实现相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android开发基础之创建启动界面Splash Screen的方法。。具体如下:

启动界面Splash Screen在应用程序是很常用的,往往在启动界面中显示产品Logo、公司Logo或者开发者信息,如果应用程序启动时间比较长,那么启动界面就是一个很好的东西,可以让用户耐心等待这段枯燥的时间。

Android 应用程序创建一个启动界面Splash Screen非常简单。比如创建一个工程MySample,主Acitity就叫MySample,创建另一个Activity叫 SplashScreen,用于显示启动界面,资源文件为splash.xml。至于如何制作SplashSceen界面,这不是本文章要讨论的东西,就 此略过。

SplashScreen的代码如下:

 

 
  1. package com.ctoof.android;  import android.app.Activity; 
  2. import android.content.Intent;  import android.os.Bundle; 
  3. import android.view.MotionEvent;  public class SplashScreen extends Activity { 
  4. protected boolean _active = true;  protected int _splashTime = 5000; 
  5. @Override  public void onCreate(Bundle savedInstanceState) { 
  6. super.onCreate(savedInstanceState);  setContentView(R.layout.splash); 
  7. Thread splashTread = new Thread() {  @Override 
  8. public void run() {  try { 
  9. int waited = 0;  while(_active && (waited < _splashTime)) { 
  10. sleep(100);  if(_active) { 
  11. waited += 100;  } 
  12. }  } catch(InterruptedException e) { 
  13. // do nothing  } finally { 
  14. finish();  // 启动主应用 
  15. startActivity(new Intent("com.ctoof.android.MySample.MyApp"));  stop(); 
  16. }  } 
  17. };  splashTread.start(); 
  18. }  @Override 
  19. public boolean onTouchEvent(MotionEvent event) {  if (event.getAction() == MotionEvent.ACTION_DOWN) { 
  20. _active = false;  }