如何在Android 12+(API 31+)设备设置自定义PNG启动屏?
在Android 12+中使用自定义PNG作为启动屏的解决方案
针对你需要用复杂自定义PNG作为启动屏的场景,有两种可行方案适配Android 12(API 31+):
方案一:扩展SplashScreen库实现自定义背景
利用官方SplashScreen库的退出动画监听,在启动阶段加载自定义PNG背景,既符合系统规范又满足自定义需求:
修改SplashScreen主题配置
将基础主题的背景设为透明,避免和自定义PNG冲突:<style name="Theme.App.Starting" parent="Theme.SplashScreen"> <item name="windowSplashScreenBackground">@android:color/transparent</item> <item name="postSplashScreenTheme">@style/Theme.App</item> </style>在启动Activity中添加自定义逻辑
初始化SplashScreen后,通过setOnExitAnimationListener添加自定义View显示PNG:class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val splashScreen = installSplashScreen() // 加载自定义PNG布局 val splashView = LayoutInflater.from(this).inflate(R.layout.splash_custom, null) splashScreen.setOnExitAnimationListener { viewProvider -> val rootView = viewProvider.view rootView.addView(splashView) // 自定义退出动画 splashView.animate() .alpha(0f) .setDuration(300L) .withEndAction { rootView.removeView(splashView) viewProvider.remove() } } setContentView(R.layout.activity_main) } }创建自定义启动屏布局(res/layout/splash_custom.xml)
直接用ImageView加载你的PNG:<ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/custom_splash_png" android:scaleType="centerCrop" />
方案二:兼容旧启动屏方式(适配Android 12规范)
如果完全不想用SplashScreen库,可以创建一个透明的启动Activity,直接显示PNG,但需要注意避免系统强制替换启动屏:
创建启动屏Activity布局(res/layout/activity_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/custom_splash_png" android:scaleType="centerCrop" />配置启动屏Activity的主题
确保主题透明,避免白屏:<style name="Theme.Splash.Transparent" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@null</item> </style>配置AndroidManifest.xml
将这个Activity设为启动Activity,并排除在最近任务列表:<activity android:name=".SplashActivity" android:theme="@style/Theme.Splash.Transparent" android:excludeFromRecents="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" />在SplashActivity中实现跳转逻辑
class SplashActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_splash) // 延迟跳转主Activity,可按需调整时长 Handler(Looper.getMainLooper()).postDelayed({ startActivity(Intent(this, MainActivity::class.java)) finish() }, 1000L) } }
注意事项
- 确保PNG适配不同屏幕分辨率,可放在
drawable-xhdpi、drawable-xxhdpi等目录避免拉伸模糊 - 方案二中,启动屏显示时长不要过长,避免影响应用启动速度
- 优先使用方案一,更符合Android 12+的启动屏设计规范,减少系统兼容性问题
内容的提问来源于stack exchange,提问作者kirvel
相关产品推荐
相关产品推荐

