歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發入門教程:Splash的實現

Android開發入門教程:Splash的實現

日期:2017/3/1 10:10:16   编辑:Linux編程

什麼是Splash

Splash也就是應用程序啟動之前先啟動一個畫面,上面簡單的介紹應用程序的廠商,廠商的LOGO,名稱和版本等信息,多為一張圖片,顯示幾秒鐘後會自動消息,然後顯示出應用程序的主體頁面。在PC上,很常見各種平台的應用程序都會有,多半是一張圖片顯示在屏幕中央,如Microsoft Office系列,或者GIMP等。在各種游戲中Splash是最常見的,幾乎所有的游戲開始都會有一張全屏的圖片,上面通常都顯示廠商的LOGO,游戲的名稱等。在手機平板等移動設備上,類似PC的Splash很少,起碼對於Android和iOS來講原生的應用程序都沒有這種Splash,但是不知從何時起,這種Splash開始在第三方應用中流行起來,幾乎所有的第三方應用程序都有啟動Splash。這些Splash的特點是占滿整個屏幕,上面LOGO,廠商的名字,應用的名字版本等,大約3到5秒後,Splash自動消失,應用主頁面顯示出來。很多應用在Splash頁面也顯示加載過程。

下面談談在Android中如何實現Splash以及它的優缺點:

使用Activity作為Splash

這可能也是最常用的方式,方法就是用一個Activity,給它設置一個背景,或者要顯示的信息(廠商,LOGO,名字和版本),讓它顯示幾秒種,然後finish()掉,並啟動應用主體Activity。

  1. <activity android:name=".SplashActivity"
  2. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
  3. android:noHistory="true"
  4. android:configChanges="orientation|keyboardHidden"
  5. android:label="@string/app_name">
  6. <intent-filter>
  7. <action android:name="android.intent.action.MAIN" />
  8. <category android:name="android.intent.category.LAUNCHER" />
  9. </intent-filter>
  10. </activity>

  1. public class SplashActivity extends Activity {
  2. private Handler mMainHandler = new Handler() {
  3. @Override
  4. public void handleMessage(Message msg) {
  5. Intent intent = new Intent(Intent.ACTION_MAIN);
  6. intent.setClass(getApplication(), NotTomorrowActivity.class);
  7. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  8. startActivity(intent);
  9. // overridePendingTransition must be called AFTER finish() or startActivity, or it won't work.
  10. overridePendingTransition(R.anim.activity_in, R.anim.splash_out);
  11. }
  12. };
  13. @Override
  14. public void onCreate(Bundle icicle) {
  15. super.onCreate(icicle);
  16. getWindow().setBackgroundDrawableResource(R.drawable.kg);
  17. mMainHandler.sendEmptyMessageDelayed(0, 5000);
  18. }
  19. // much easier to handle key events
  20. @Override
  21. public void onBackPressed() {
  22. }
  23. }
Copyright © Linux教程網 All Rights Reserved