創(chuàng)建一個LaunchActivity不要用setContentView()方法進行渲染(耗時),通過Theme添加背景樣式即可
public class LaunchActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
創(chuàng)建drawable文件作為LaunchActivity的Theme的背景
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 底層白色 -->
<item android:drawable="@color/white" />
<!-- 頂層Logo居中 -->
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/start_page_welcome" />
</item>
</layer-list>
在styles中為LaunchActivity創(chuàng)建樣式
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/bg_launch</item>
</style>
在AndroidManifest.xml文件中LaunchActivity注冊部分如下配置
<activity android:name=".activity.LaunchActivity"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
注意:如果AppTheme中寫了<item name="android:windowIsTranslucent">true</item>屬性,可能會導(dǎo)致點擊圖標后有延遲依然停留在桌面,參考下面AppTheme寫法
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:listDivider">@drawable/default_recycler_line</item>
<!--<item name="android:windowIsTranslucent">true</item>-->
<item name="android:windowNoTitle">true</item>
</style>