You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android:如何在新SplashScreen API中为windowSplashScreenBackground设置Drawable并实现带渐变背景与Logo的启动页

解决SplashScreen API实现渐变背景+Logo的问题

你遇到的这个问题很典型——Android的SplashScreen API设计上,windowSplashScreenBackground确实只支持颜色值,直接设置drawable资源会失效;而替换android:windowBackground又会覆盖默认的SplashScreen图标层,导致Logo消失。别担心,下面有两种可行的解决方案,推荐第一种更简洁的:

这种方法通过创建一个包含渐变背景和Logo的layer-list drawable,直接作为启动页的背景,同时隐藏默认的SplashScreen图标,避免重复显示:

步骤1:创建Layer-List Drawable

res/drawable/目录下新建splash_gradient_with_logo.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 第一层:渐变背景 -->
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#FF6B6B" <!-- 替换成你的起始渐变色 -->
                android:endColor="#4ECDC4"   <!-- 替换成你的结束渐变色 -->
                android:angle="180"          <!-- 控制渐变方向:0=左到右,90=上到下,180=右到左 -->
                android:type="linear"/>      <!-- 可选:radial(径向)或sweep(扫描)渐变 -->
        </shape>
    </item>
    <!-- 第二层:居中显示的Logo -->
    <item
        android:drawable="@mipmap/ic_launcher" <!-- 替换成你的Logo资源 -->
        android:gravity="center"
        android:top="24dp" <!-- 可选:调整Logo的垂直位置,上下留空 -->
        android:bottom="24dp"/>
</layer-list>

步骤2:配置SplashScreen主题

修改你的Theme.MyApp.SplashScreen主题,把android:windowBackground设为上面的layer-list,同时将默认图标设为@null

<style name="Theme.MyApp.SplashScreen" parent="Theme.SplashScreen">
    <!-- 禁用默认的SplashScreen图标,避免和我们自定义的Logo重复 -->
    <item name="windowSplashScreenAnimatedIcon">@null</item>
    <!-- 设置包含渐变和Logo的layer-list作为整个启动页背景 -->
    <item name="android:windowBackground">@drawable/splash_gradient_with_logo</item>
    <!-- 指定启动页结束后跳转的主应用主题 -->
    <item name="postSplashScreenTheme">@style/Theme.MyApp</item>
</style>

步骤3:启动Activity配置

确保你的启动Activity中正确调用installSplashScreen()(Android 12+要求):

class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // 必须在super.onCreate之前调用
        installSplashScreen()
        super.onCreate(savedInstanceState)
        
        // 这里添加你的启动逻辑,比如延迟跳转或初始化操作
        Handler(Looper.getMainLooper()).postDelayed({
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }, 2000) // 示例:2秒后跳转主页面
    }
}

方案2:自定义SplashScreen背景(保留默认动画)

如果你想保留SplashScreen的默认图标动画效果,同时添加渐变背景,可以通过代码动态添加背景层:

步骤1:创建单独的渐变背景Drawable

res/drawable/目录下新建gradient_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FF6B6B"
        android:endColor="#4ECDC4"
        android:angle="180"/>
</shape>

步骤2:配置主题

保留默认的图标设置,同时把windowSplashScreenBackground设为透明:

<style name="Theme.MyApp.SplashScreen" parent="Theme.SplashScreen">
    <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
    <item name="windowSplashScreenBackground">@android:color/transparent</item>
    <item name="postSplashScreenTheme">@style/Theme.MyApp</item>
</style>

步骤3:代码中添加渐变背景

在启动Activity中,获取SplashScreen的View,把渐变背景添加到底层:

class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val splashScreen = installSplashScreen()
        super.onCreate(savedInstanceState)
        
        // 创建渐变背景View
        val gradientBackground = View(this).apply {
            background = resources.getDrawable(R.drawable.gradient_background, theme)
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        }
        
        // 将渐变背景添加到SplashScreen视图层级的最底部,确保在图标下方
        splashScreen.view.addView(gradientBackground, 0)
        
        // 启动逻辑
        Handler(Looper.getMainLooper()).postDelayed({
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }, 2000)
    }
}

这样既保留了SplashScreen的默认图标动画,又实现了渐变背景效果。


内容的提问来源于stack exchange,提问作者Almaz_KhR

火山引擎 最新活动