首页 > 系统 > Android > 正文

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

2019-10-24 20:35:43
字体:
来源:转载
供稿:网友

这篇文章主要介绍了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; 
  2. import android.app.Activity; 
  3. import android.content.Intent; 
  4. import android.os.Bundle; 
  5. import android.view.MotionEvent; 
  6. public class SplashScreen extends Activity { 
  7. protected boolean _active = true
  8. protected int _splashTime = 5000; 
  9. @Override 
  10. public void onCreate(Bundle savedInstanceState) { 
  11. super.onCreate(savedInstanceState); 
  12. setContentView(R.layout.splash); 
  13. Thread splashTread = new Thread() { 
  14. @Override 
  15. public void run() { 
  16. try { 
  17. int waited = 0; 
  18. while(_active && (waited < _splashTime)) { 
  19. sleep(100); 
  20. if(_active) { 
  21. waited += 100; 
  22. catch(InterruptedException e) { 
  23. // do nothing 
  24. finally { 
  25. finish(); 
  26. // 启动主应用 
  27. startActivity(new Intent("com.ctoof.android.MySample.MyApp")); 
  28. stop(); 
  29. }; 
  30. splashTread.start(); 
  31. @Override 
  32. public boolean onTouchEvent(MotionEvent event) { 
  33. if (event.getAction() == MotionEvent.ACTION_DOWN) { 
  34. _active = false
  35. return true

然后在AndroidMainfest.xml中修改代码如下:

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3. package="com.ctoof.android" 
  4. android:versionCode="1" 
  5. android:versionName="1.0"
  6. <application android:icon="@drawable/icon" android:label="@string/app_name"
  7. <activity android:name=".SplashScreen" 
  8. android:label="@string/app_name"
  9. <intent-filter> 
  10. <action android:name="android.intent.action.MAIN" /> 
  11. <category android:name="android.intent.category.LAUNCHER" /> 
  12. </intent-filter> 
  13. </activity> 
  14. <activity android:name=".MyApp"
  15. <intent-filter> 
  16. <action android:name=" com.ctoof.android. MySample.MyApp " /> 
  17. <category android:name="android.intent.category.DEFAULT" /> 
  18. </intent-filter> 
  19. </activity> 
  20. </application> 
  21. <uses-sdk android:minSdkVersion="4" /> 
  22. </manifest> 

在这里负责注册两个活动。把负责管理启动界面Splash Screen的活动Activity作为应用程序的主活动,然后在SplashScreen中负责启动MyApp。

希望本文所述对大家的Android程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表