public class SplashscreenActivity extends Activity {
public final static String FIRST_RUN_PREFERENCE = "first_run";
private Animation endAnimation;
private Handler endAnimationHandler;
private Runnable endAnimationRunnable;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@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();
}
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);
}
}
}
动画xml文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:zAdjustment="bottom" android:fillAfter="false">
<alpha android:fromAlpha="1.0" android:toAlpha="0"
android:duration="400" />
</set>
1、Animation类的使用
①定义好xml资源文件
②load
endAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
endAnimation.setFillAfter(true); //if true, the transformation that this animation performed will persist when it is finished.
③startAnimation,只要是View就可以用
findViewById(R.id.splashlayout).startAnimation(endAnimation);
④若要对动画的状态做处理,只要设置setAnimationListener即可。
endAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
}
});
2、若只想延时来做某个动作,只要用Handler类的postDelayed即可
Handler endAnimationHandler = new Handler();
endAnimationHandler.removeCallbacks(endAnimationRunnable);
endAnimationHandler.postDelayed(endAnimationRunnable, 2000);
3、PreferenceManager首选项的使用
PreferenceManager类似于VC下的ini配置文件
SharedPreferences mPerferences = PreferenceManager.getDefaultSharedPreferences(this);
//read
int counter = mPerferences.getInt("counter", 0);
Log.i("zxd", "This app has been started " + counter + " times.");
//write
SharedPreferences.Editor mEditor = mPerferences.edit();
mEditor.putInt("counter", ++counter);
mEditor.commit();