做过Splash的都知道,一般的做法是在style中设置windowBackground为启动图,来避免冷启动时的黑屏,但是如果放一张固定尺寸的图在某些屏幕上就会出现拉伸,并且windowBackground还不能centerCrop,就算通过资源限定符也不能完美的适配。
解决方案:layer-list
style.xml
<style name="AppTheme.Splash" parent="AppTheme">
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:windowNoTitle">true</item>
</style>
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--白色矩形 作为背景色-->
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>
<!--单独的slogan图片 并且设置下间距-->
<item>
<!--位置设置成靠下-->
<bitmap
android:gravity="center"
android:src="@drawable/splash_bg" />
</item>
</layer-list>
适配启动图的核心代码就是layer-list,将元素叠成启动图的样式,更复杂一点的启动图也是可以适配的。
参考:https://github.com/android-cn/android-discuss/issues/715
参考:https://blog.csdn.net/aa464971/article/details/86692198




