對(duì)于比較大型的應(yīng)用敲长,啟動(dòng)的時(shí)候(特別是冷啟動(dòng))會(huì)顯示一段時(shí)間黑屏或白屏(和應(yīng)用的theme有關(guān))梨与,對(duì)用戶很不友好,一般來(lái)說(shuō)未舟,簡(jiǎn)單的處理方式有以下兩種
<item name="android:windowDisablePreview">true</item>
不顯示啟動(dòng)界面
<item name="android:windowIsTranslucent">true</item>
把啟動(dòng)界面的背景設(shè)成透明的
但是這種處理方案有個(gè)很嚴(yán)重的問(wèn)題圈暗,就是點(diǎn)了圖標(biāo)之后,會(huì)卡在主界面一段時(shí)間再顯示應(yīng)用界面裕膀,實(shí)際上因?yàn)閼?yīng)用加載導(dǎo)致的
主流大應(yīng)用的處理方法
- 自定義主題
- 將啟動(dòng)activity的theme設(shè)置為自定義主題
- 在啟動(dòng)activity的onCreate方法中员串,在super.onCreate之前將theme設(shè)置回去
先定義一個(gè)theme
<style name="LaunchTheme">
<item name="android:windowBackground">@drawable/launch_layout</item>
<item name="android:windowFullscreen">true</item>
<item name="windowNoTitle">true</item>
</style>
定義啟動(dòng)界面顯示的內(nèi)容
launch_layout
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The background color, preferably the same as your normal theme -->
<item android:drawable="@android:color/holo_red_dark"/>
<item android:top="150dp">
<bitmap android:gravity="top"
android:src="@drawable/girl" />
</item>
</layer-list>
定義一個(gè)啟動(dòng)的activity
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}).start();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
activity_splash
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/music"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"/>
</RelativeLayout>
將啟動(dòng)的activity主題設(shè)為我們的啟動(dòng)主題
<activity android:name="com.example.optimizes.lunchapp.SplashActivity"
android:theme="@style/LaunchTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>