Android 12启动页API:将方形Logo移至中心线上方且不缩小
Android Splash Screen API 自定义问题解答
1. 能否用新API实现方形Logo居中线上方且不缩小?
可以实现,但需要通过自定义启动页视图调整突破默认约束,具体操作如下:
- 先在应用主题中配置基础启动页参数,确保框架正常工作:
<style name="Theme.App.Starting" parent="Theme.SplashScreen"> <item name="windowSplashScreenBackground">@color/splash_bg</item> <item name="windowSplashScreenAnimatedIcon">@drawable/ic_square_logo</item> <item name="postSplashScreenTheme">@style/Theme.App</item> </style> - 在Activity的
onCreate方法中,获取SplashScreen实例,通过监听退出动画时机拿到可操作的启动页视图,调整Logo的位置和尺寸:override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val splashScreen = installSplashScreen() splashScreen.setOnExitAnimationListener { splashScreenView -> // 定位系统默认的Logo视图 val logoView = splashScreenView.findViewById<View>(com.google.android.material.R.id.icon) // 上移Logo(距离用dp定义在dimens.xml中,适配不同屏幕) val translateY = resources.getDimensionPixelOffset(R.dimen.logo_translate_y) logoView.translationY = -translateY.toFloat() // 强制保持原尺寸,禁止系统缩放 logoView.scaleX = 1f logoView.scaleY = 1f // 执行退出动画并移除启动页 splashScreenView.animate() .alpha(0f) .withEndAction { splashScreenView.remove() } .start() } setContentView(R.layout.activity_main) } - 注意:平移距离建议用dp单位定义,避免在不同密度屏幕上出现位置偏差;如果不需要退出动画,也可直接调整视图后立即移除启动页。
2. 采用旧方案是否违反API文档要求?
- 文档中提到的“Android 11及以下版本的自定义启动页需迁移至SplashScreen API”属于最佳实践建议,并非强制约束,但存在明显问题:
- Android 12及以上系统会自动将旧启动页(如自定义Theme+布局)适配为系统默认的Splash Screen样式,系统会提取旧启动页背景色,并在中心叠加默认应用图标,完全破坏自定义布局的UX设计
- 旧方案在Android 12+上会出现启动页切换闪烁的问题,用户体验更差
- 因此不建议退回旧方案,基于新API做自定义调整是更合规且能保证UX一致性的选择
内容的提问来源于stack exchange,提问作者kallis
相关产品推荐
相关产品推荐

