Android适配Splash页面

博客介绍了Android背景图的适配方法。包括直接用图片作android:windowBackground属性值、制作并使用点9图作该属性值,还提及利用layer - list方式适配,最后给出了DEMO源码地址。
原始图片

一、适配背景图

1.1 直接使用图片作为android:windowBackground属性值
<resources>
    <!-- 直接在清单文件的application节点下面android:theme使用-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@drawable/blue_circle</item>
    </style>
</resources>
1.2 制作点9图
1.3 使用点9图片作为android:windowBackground属性值
<resources>
    <!-- 直接在清单文件的application节点下面android:theme使用-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@drawable/blue_circle1</item>
    </style>
</resources>

二、利用layer-list方式适配

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@drawable/splashxml</item>
    </style>

</resources>

splashxml.xml源码如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="fill">
        <shape android:shape="rectangle">
            <solid android:color="#ff0" />
        </shape>
    </item>
    <item>
        <!--
        android:gravity设置bitmap图像的展示方式,居中/填充/水平居中/垂直居中等等
        android:tileMode设置图片的展示方式
        "clamp"表示按照周边进行绘制
        "repeat"重复展示图片
        "mirror"镜像展示图片
        使用这个属性可能导致无效android:gravity
              -->
        <bitmap
            android:gravity="center"
            android:src="@drawable/blue_circle"
            android:tileMode="clamp"/>
    </item>
</layer-list>

不使用 android:tileMode="clamp"的效果图如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="fill">
        <shape android:shape="rectangle">
            <solid android:color="#ff0" />
        </shape>
    </item>
    <item>
        <!--
        android:gravity设置bitmap图像的展示方式,居中/填充/水平居中/垂直居中等等
        android:tileMode设置图片的展示方式
        "clamp"表示按照周边进行绘制
        "repeat"重复展示图片
        "mirror"镜像展示图片
        使用这个属性可能导致无效android:gravity
              -->
        <bitmap
            android:gravity="center"
            android:src="@drawable/blue_circle"/>
    </item>
</layer-list>

三、总结

要想改变android:windowBackground的属性值,最佳效果是把小切图与背景图分开,再用layer-list来处理,否则很容易变形。点9图来做适配变形也只能适用于简单的图片。

DEMO源码地址

<think>我们讨论的是如何在Android应用中移除启动时的默认Splash页面(即启动画面)。在Android中,Splash页面通常由主题(Theme)设置,默认情况下,应用启动时会显示一个由系统根据主题中的windowBackground属性绘制的初始页面,直到应用的第一帧绘制完成。 要移除这个Splash页面,我们可以通过修改主题,将windowBackground设置为透明或者与主界面背景一致,从而避免出现一个明显的Splash效果。但请注意,完全移除可能会导致启动时出现白屏或黑屏(取决于主题背景色),因此有时我们选择将其设置为透明,这样在应用启动时用户会直接看到主界面,但主界面加载需要时间,可能会在启动初期有短暂的空白。 另一种常见的做法是自定义Splash主题,但这里要求去掉,所以我们可以设置透明背景。 具体步骤如下: 1. 在res/values/styles.xml中,修改或创建一个主题(通常是AppTheme,或者是专门用于启动的主题)。 2. 在该主题中设置`<item name="android:windowBackground">@android:color/transparent</item>`或者设置一个与主界面背景相同的颜色或图片。 但是注意:如果应用的主题继承自Theme.MaterialComponents等,可能需要调整。 另外,在Android 12(API级别31)及以上,系统强制要求使用SplashScreen API,所以对于新版本,我们需要适配新的SplashScreen API,但如果我们不想显示任何Splash,我们可以设置一个透明的背景和图标。 下面是一个示例,修改主题来去掉Splash页面(即设置为透明背景): 在styles.xml中: ```xml <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 设置状态栏颜色 --> <item name="android:statusBarColor">@color/colorPrimaryDark</item> <!-- 设置窗口背景为透明 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 其他属性 --> </style> ``` 然后,在AndroidManifest.xml中,将应用的主题设置给启动Activity: ```xml <activity android:name=".MainActivity" android:theme="@style/AppTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ``` 对于Android 12及以上,我们需要使用SplashScreen API,并且可以通过以下方式设置透明背景: 首先,确保在styles.xml中为Android 12以上设置一个透明的启动画面背景: 在res/values-v31/styles.xml中: ```xml <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 设置SplashScreen的背景为透明 --> <item name="android:windowSplashScreenBackground">@android:color/transparent</item> <!-- 设置中心图标的动画为无 --> <item name="android:windowSplashScreenAnimatedIcon">@null</item> <!-- 设置动画持续时间,但为了立即消失,可以设置为0 --> <item name="android:windowSplashScreenAnimationDuration">0</item> <!-- 其他属性 --> </style> ``` 注意:在Android 12中,完全移除Splash画面可能不太现实,因为系统会强制有一个SplashScreen,但我们可以通过设置透明背景和移除图标来使其看起来像是没有Splash。 另外,如果应用启动时间较长,用户可能会看到一段时间的空白,所以通常建议保留一个简单的启动画面以提升体验。 但根据问题要求,我们是要去掉默认的图标和Splash页面,所以通过设置透明背景和移除图标,可以达到几乎看不到Splash的效果。 总结:通过修改主题的windowBackground(对于旧版本)或使用SplashScreen API设置透明背景(对于新版本),可以去掉默认的Splash页面。 现在,根据规则,在回答后提出3-5个相关问题。</think>### 如何移除 Android App 的默认 Splash 页面 要移除 Android 应用启动时的默认 Splash 页面(系统根据主题自动生成的启动图),需通过修改主题配置实现。以下是具体步骤: 1. **修改应用主题** 在 `res/values/styles.xml` 中,将主题的 `windowBackground` 设置为透明或纯色背景: ```xml <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="android:windowBackground">@android:color/transparent</item> <!-- 透明背景 --> <!-- 或使用纯色:<item name="android:windowBackground">@color/your_color</item> --> </style> ``` 2. **应用主题到启动 Activity** 在 `AndroidManifest.xml` 中确保启动 Activity 使用该主题: ```xml <activity android:name=".MainActivity" android:theme="@style/AppTheme"> <!-- 应用自定义主题 --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ``` 3. **适配 Android 12+**(API 31+) 针对新系统,在 `res/values-v31/styles.xml` 中额外配置: ```xml <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="android:windowSplashScreenBackground">@android:color/transparent</item> <item name="android:windowSplashScreenAnimatedIcon">@null</item> <!-- 移除默认图标 --> </style> ``` **效果**: 应用启动时将直接显示主界面,不再展示系统生成的 Splash 页面(白屏/图标动画消失)。 **注意事项**: - 此方法移除了系统默认启动图,但不会影响自定义 Splash Activity - 若需完全跳过启动延迟,可结合 `android:windowDisablePreview`(不推荐,可能导致界面闪烁)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值