很多应用程序启动时都需要一个页面展示公司品牌或者Logo,或者即便不需要这个页面,如果布局比较复杂,启动应用的时候会有一个短暂的白屏(跟系统主题相关),往往我们会单独写一个Activity,然后显示一张图片或者Logo,设置固定的时间延迟启动真正的MainActivity,这样未免小题大做,分享一个小技能:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="@color/background_material_light"/>
</item>
<item>
<bitmap
android:src="@drawable/launch_logo_p_w_picpath"
android:tileMode="disabled"
android:gravity="center"/>
</item>
</layer-list>
定义底色和一个bitmap展示Logo,Logo居中显示,超出bitmap大小的区域不进行平铺或者其他处理;
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" />
<style name="AppTheme.BrandedLaunch" parent="AppTheme">
<item name="android:windowBackground">@drawable/branded_logo</item>
</style>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.BrandedLaunch">
由于这样会改变整体的主题,想回到原来的系统默认主题可以在onCreate中动态更换:
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
之前会在应用启动前,显示一个短暂的白屏:

使用这种方式后:

是不是舒服很多。
参考自antonioleiva,源码Github repository