在 Android 上使用Qt Quick 实现闪屏

展示如何在 Android 上使用Qt Quick View 设置闪屏。

这个简单的示例演示了如何将 QML 视图用作 Qt 闪屏。没有使用额外的过渡效果。在此示例中,隐藏了 Android 12 中引入的默认闪屏。应用程序启动时,闪屏会显示两秒钟,之后主视图将不带过渡地显示出来。

本示例是演示闪屏的两个类似示例系列之一。其中一个使用 QML 视图,另一个使用 AndroidManifest 标志。

在 Android 上使用Qt Quick 闪屏

要将 QML 视图用作闪屏,您需要定义一个视图,并添加一些闪屏功能的特定项目。

Window {
    id: splash
    color: "#2CDE85"
    title: qsTr("Splash Window")
    modality: Qt.ApplicationModal
    flags: Qt.SplashScreen
    visible: true

    property int timeoutInterval: 2000
    signal timeout
  • modality: Qt.ApplicationModal 设置视图模态。
  • flags: Qt.SplashScreen 隐藏标题栏
  • property int timeoutInterval: 2000 设置视图的可见时间。
加载主视图
    Loader {
        id: mainLoader
        source: "Main.qml"

计时器会在超时时调用退出函数,从闪屏切换到主视图。任何可能的退出动画都将在此启动。

    function exit() {
        mainLoader.item.show();
        splash.visible = false
        splash.timeout()
    }

计时器会留出适当的时间让闪屏出现。这可以与 Loader::onLoaded() 信号结合使用,也可以在应用程序准备好显示时与其他来自应用程序实现的信号结合使用。

    Timer {
        interval: splash.timeoutInterval;
        running: splash.visible;
        repeat: false
        onTriggered: {
            splash.exit()
        }
    }

启动时,在显示应用程序的第一个视图之前会显示默认的闪屏。有关修改的说明,请参阅Android 闪屏
默认情况下,Android 会将应用程序图标用于闪屏。在 Qt for Android 应用程序中,可以将其修改为应用程序闪屏,或与 Qt 闪屏结合使用。

隐藏本地 Android 闪屏

为了保持平台间启动用户体验的一致性,可能需要隐藏 Android 闪屏。为此,可定义一个半透明主题来有效隐藏 Android 闪屏。

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="splashStartTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowDisablePreview">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>
</resources>

但这有一个问题。如果使用半透明主题,方向更改将停止工作。这是因为 Android 视图从底层视图获取方向,而底层视图通常是不会改变方向的应用程序抽屉。由于在设置 Activity 视图后无法更改主题,应用程序最终只能停留在一个方向上。
作为一种变通方法,我们可以为 Android 闪屏创建带有半透明主题的一次性 Activity。我们可以尽快启动另一个 Activity,它具有应用程序所需的所有功能。

        <activity
            android:name=".SplashActivity"
            android:exported="true"
            android:label="QML Splash Screen Example"
            android:theme="@style/splashStartTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

现在,原生的 Android 闪屏不会显示,而我们启动的 QtActivity 会显示 QML 中定义的闪屏。要启动 QML 闪屏,我们必须覆盖 Android Activity 的onCreate 方法,并使用 Intent 来启动 QtActivity。

// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
package io.qt.qmlsplashscreeninandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.content.Intent;
import android.view.ViewTreeObserver;
import org.qtproject.qt.android.bindings.QtActivity;

public class SplashActivity extends Activity {

    private static final String TAG = "SplashActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set up an OnPreDrawListener to the root view.
        final View content = findViewById(android.R.id.content);
                content.getViewTreeObserver().addOnPreDrawListener(
                        new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                Intent i = new Intent(SplashActivity.this, QtActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(i);

                content.getViewTreeObserver().removeOnPreDrawListener(this);
                finish();
                return true;
            }
        });
    }
}

最后,QML 闪屏会打开主视图,本例中主视图只包含一个带关闭按钮的 View。

示例项目 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.