如何使用Android Splash Screen API设置全屏启动图
解决Splash Screen API实现全屏启动图的问题
方案1:通过主题配置直接实现全屏背景
Splash Screen API默认的图标区域会居中显示,想要实现全屏图效果,可将全屏图设为启动主题的背景,同时把图标替换为透明占位图,让背景完全展示:
- 先创建一个透明占位Drawable(
res/drawable/transparent_placeholder.xml):
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/transparent"/> </shape>
- 在Splash主题中配置参数:
<style name="Theme.App.SplashScreen" parent="Theme.SplashScreen"> <!-- 设置全屏启动图为主题背景 --> <item name="windowSplashScreenBackground">@drawable/full_screen_splash</item> <!-- 用透明占位图替代默认居中图标 --> <item name="windowSplashScreenAnimatedIcon">@drawable/transparent_placeholder</item> <!-- 移除图标区域的背景色 --> <item name="windowSplashScreenIconBackgroundColor">@android:color/transparent</item> <!-- 指定启动完成后切换的主主题 --> <item name="postSplashScreenTheme">@style/Theme.App</item> </style>
- 注意:全屏图要适配不同设备分辨率,优先使用VectorDrawable或9-Patch图,避免拉伸变形。
方案2:监听Splash退出动画,替换自定义全屏View
如果主题配置在部分机型存在适配问题,可通过代码监听Splash退出时机,先显示自定义全屏启动图,再平滑过渡到主界面:
- 创建全屏启动图布局(
res/layout/full_screen_splash.xml):
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/full_screen_splash" android:scaleType="centerCrop"/>
- 在启动Activity中实现过渡逻辑:
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 先加载自定义全屏启动图 val splashView = LayoutInflater.from(this).inflate(R.layout.full_screen_splash, null) setContentView(splashView) // 初始化系统Splash Screen val splashScreen = installSplashScreen() splashScreen.setOnExitAnimationListener { splashScreenView -> // 执行自定义退出动画,完成后切换到主界面 val fadeOutAnim = ObjectAnimator.ofFloat(splashView, View.ALPHA, 1f, 0f) fadeOutAnim.duration = 500 fadeOutAnim.doOnEnd { splashView.removeFromParent() setContentView(R.layout.activity_main) } fadeOutAnim.start() // 移除系统默认的Splash视图 splashScreenView.remove() } } }
关于拆分图片的问题
windowSplashScreenBrandingImage有固定的尺寸和位置限制,用来拼接全屏图很容易出现变形、错位,不建议用这种方式实现全屏效果,优先选择上面两种方案。
内容的提问来源于stack exchange,提问作者Md Anas Shikoh
相关产品推荐
相关产品推荐

