啓動動畫的原理:程序啓動後加載一個只有一個圖片的activity頁面,該頁面佔滿全屏。一段時間後,關閉當前activity進入主頁面或登錄頁面的activity。
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
SplashActivity.this.startActivity(intent);//啓動新頁面
SplashActivity.this.finish();//啓動頁面不需要壓棧回退
}
}, 1000);//當前頁面停留時間大約爲1S
this.overridePendingTransition(R.anim.enter_alpha, R.anim.exit_alpha);//頁面跳轉動畫設置
}
}
動畫切換的資源文件放置在res/anim文件夾。
exit_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="500"/>
</set>
enter_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="500"/>
</set>
Splash採用了一個圖片佔滿全屏,可用多種方式實現,例如可用一個層佈局空間,將圖片設置爲其背景:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
Android:background="@drawable/yancao"
tools:context=".SplashActivity" >
</RelativeLayout>
需要注意的一點是,activity剛顯示的並不是顯示的xml文件佈局所見的界面,而是它的主題頁面,當activity調用onCreate函數後才重繪了xml佈局的界面。所以你的splash很可能會閃出如下右邊界面再轉爲右邊xml文件定義的splash界面。
這種頁面切換導致的和軟件整體風格不和諧畫面是應該儘量避免,方法就是設置這個splashactivity的主題。上面左圖的主題是:Theme.Light ,這個主題界面背景默認是白色的有標題欄;如果將主題改成:Theme.NoTitlebar ,默認是黑色背景無標題欄。修改之後,在進入定義的splash圖片之前,屏幕是全黑的。