我們從AndroidManifest.xml文件中看到有這么一段。
<activity android:name=".activity.SplashscreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
知道歡迎界面為SplashscreenActivity,然后我們再來看看其類結構,以及依賴項,如下:
我們可以看到有一個showTutorial()方法來彈出軟件說明對話框,以及Animation,Handler,Runnable等屬性,用來產生動畫,以及動畫后與主UI線程通信。
首先我們來看OnCreate方法

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splashscreen);
findViewById(R.id.splashlayout);
endAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
endAnimation.setFillAfter(true);
endAnimationHandler = new Handler();
endAnimationRunnable = new Runnable() {
@Override
public void run() {
findViewById(R.id.splashlayout).startAnimation(endAnimation);
}
};
endAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
HomeActivity.launch(SplashscreenActivity.this);
SplashscreenActivity.this.finish();
}
});
showTutorial();
}
首先通過AnimationUtils.loadAnimation靜態方法加載anim下的淡出動畫文件fade_out,並設置動畫完成后填充(這里還未播放動畫)。
然后通過一個定義Runnable對象,在線程中開始這個動畫,同時添加動畫監聽。回調方法有:onAnimationStart,onAnimationRepeat,onAnimationEnd。(這里還未開啟線程)。
當然它直接調用了showTutorial();彈出軟件說明對話框。

final void showTutorial() {
boolean showTutorial = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(FIRST_RUN_PREFERENCE, true);
if (showTutorial) {
final TutorialDialog dlg = new TutorialDialog(this);
dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
CheckBox cb = (CheckBox) dlg.findViewById(R.id.toggleFirstRun);
if (cb != null && cb.isChecked()) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SplashscreenActivity.this);
prefs.edit().putBoolean(FIRST_RUN_PREFERENCE, false).commit();
}
endAnimationHandler.removeCallbacks(endAnimationRunnable);
endAnimationHandler.postDelayed(endAnimationRunnable, 2000);
}
});
dlg.show();
} else {
endAnimationHandler.removeCallbacks(endAnimationRunnable);
endAnimationHandler.postDelayed(endAnimationRunnable, 1500);
}
}
看到boolean showTutorial = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(FIRST_RUN_PREFERENCE, true);這一句,從Preference中得到共享的全局配置信息,這里是保存是否彈出對話框。
至於PreferenceManager怎么使用,可以參考下我的一篇Preference設置文章。主要是以鍵值對方式保存值。
然后實例化對話框TutorialDialog,並且添加關閉對話框setOnDismissListener監聽,在關閉對話框時候觸發回調,我們再回調函數public void onDismiss(DialogInterface dialog)中,可以看到首先獲取是否選中選項並且保存到Preference中,然后移除回調endAnimationRunnable,同時執行了endAnimationRunnable線程。
這里的執行方法是:endAnimationHandler.postDelayed(endAnimationRunnable, 2000); 就是間隔2秒后執行我們的線程,通過Handler執行。
執行線程,在線程中我們重寫它的run方法,在這個方法中,我們可以看到它執行的是播放anim動畫。
同時在播放動畫完畢后,跳轉到另外一個界面,就是我們的主界面。HomeActivity
@Override
public void onAnimationEnd(Animation animation) {
HomeActivity.launch(SplashscreenActivity.this);
SplashscreenActivity.this.finish();
}
當然,我們可以進入這個launch方法,可以看到它都是通過Intent進行界面跳轉的。
其中: intent.setFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP) 表示,開啟目標activity時,會清理棧中的其他activity.
public static void launch(Context c){
Intent intent = new Intent(c, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
c.startActivity(intent);
}
接下來,我們看看軟件說明這個對話框。

public class TutorialDialog extends Dialog {
/**
* @param context
*/
public TutorialDialog(Context context) {
super(context);
initialize(context);
}
/**
* @param context
* @param theme
*/
public TutorialDialog(Context context, int theme) {
super(context, theme);
initialize(context);
}
/**
* @param context
* @param cancelable
* @param cancelListener
*/
public TutorialDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
initialize(context);
}
/**
* Common initialization code
*/
private final void initialize(final Context context) {
setContentView(R.layout.tutorial);
setTitle(R.string.tutorial_title);
Button mCloseButton = (Button)findViewById(R.id.closeTutorial);
if (mCloseButton != null) {
mCloseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
}
繼承之Dialog類,然后在初始化方法initialize中找到相關的內容以及標題,並設置Button按鈕點擊監聽,在監聽方法中關閉本對話框。作者封裝了一系列的對話框在com.teleca.jamendo.dialog包中。
本文相關的界面如下: