Android 12使用Splash Screen API如何实现无裁剪启动页效果?
可以实现和Android 11完全一致的无裁剪启动页效果。
问题原因
你当前配置出现裁剪的核心原因有两点:
- Android 12的
windowSplashScreenAnimatedIcon属性是为启动页中心小图标设计的,系统会强制将该资源限制在固定尺寸的安全区内,自动裁剪超出区域的内容,直接传入全屏背景资源必然会被裁切。 - Android 12及以上版本的启动页主题需要继承系统提供的SplashScreen主题族,才能正确控制启动页的各项显示属性,你之前使用的
Theme.Light.NoTitleBar父主题无法适配新API的逻辑。
具体实现方案
1. 修改values-v31/styles.xml配置
替换你原有v31版本的样式文件内容为如下配置:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="LaunchTheme" parent="@android:style/Theme.SplashScreen.Light"> <!-- 隐藏系统默认的中心图标,避免遮挡背景内容 --> <item name="android:windowSplashScreenAnimatedIcon">@android:color/transparent</item> <!-- 关闭默认图标动画,消除多余过渡效果 --> <item name="android:windowSplashScreenAnimationDuration">0</item> <!-- 启动页背景直接沿用你原有的launch_background资源,实现无裁剪全屏显示 --> <item name="android:windowSplashScreenBackground">@drawable/launch_background</item> <item name="android:windowFullscreen">false</item> </style> <style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar"> <item name="android:windowBackground">?android:colorBackground</item> </style> </resources>
2. 沿用原有资源
你之前定义的launch_background.xml、纯白背景图、splash.png都不需要做任何修改,可直接复用。
3. 可选:解决启动页跳闪问题
如果启动页退出时出现明显的跳闪过,可以在首页Activity的onCreate方法中,setContentView之前添加如下代码,控制启动页的消失时机:
// Kotlin 示例 val splashScreen = installSplashScreen() splashScreen.setKeepOnScreenCondition { // 此处返回true时保持启动页显示,直到首页内容渲染完成后改为false即可 !isAppContentReady }
// Java 示例 SplashScreen splashScreen = SplashScreen.installSplashScreen(this); splashScreen.setKeepOnScreenCondition(() -> { // 此处返回true时保持启动页显示,直到首页内容渲染完成后改为false即可 return !isAppContentReady; });
内容的提问来源于stack exchange,提问作者Giuseppe Cianci
相关产品推荐
相关产品推荐

