You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android 12+(API 31+)设备设置自定义PNG启动屏?

在Android 12+中使用自定义PNG作为启动屏的解决方案

针对你需要用复杂自定义PNG作为启动屏的场景,有两种可行方案适配Android 12(API 31+):

方案一:扩展SplashScreen库实现自定义背景

利用官方SplashScreen库的退出动画监听,在启动阶段加载自定义PNG背景,既符合系统规范又满足自定义需求:

  1. 修改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>
    
  2. 在启动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)
        }
    }
    
  3. 创建自定义启动屏布局(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,但需要注意避免系统强制替换启动屏:

  1. 创建启动屏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" />
    
  2. 配置启动屏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>
    
  3. 配置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" />
    
  4. 在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 16:54:56