在 Android 上使用 Qt 实现闪屏

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

这是一个简单的示例,演示了使用 Qt 显示闪屏的一种方法。应用程序启动时,带有徽标和背景颜色的 Qt 闪屏会显示两秒钟。然后 Qt 闪屏淡出,显示应用程序主视图。主视图由一个简单的标签和一个关闭按钮组成。

在此示例中,Android 默认闪屏(自 Android 12 起)被隐藏。

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

在 Android 上使用 Qt 闪屏

Android 的 Qt 闪屏在 AndroidManifest.xml 中定义。Android 项目文件可手动创建或使用Qt Creator 模板生成器创建。使用Qt Creator 模板生成器创建 Android 项目文件:

  1. 打开Projects
  2. 转到Build Steps 小节
  3. 打开Build Android APK
  4. Application 子节中选择Create Templates
            <meta-data android:name="android.app.splash_screen_drawable"
                android:resource="@drawable/qtsplashscreen"/>
            <meta-data android:name="android.app.splash_screen_sticky"
                android:value="true"/>

在前面的代码段中

自 Android 12 起,Android 有了默认闪屏。启动时,默认闪屏会在显示应用程序的第一个视图之前显示。有修改该屏幕的说明和 API。请参阅Android 闪屏
默认情况下,Android 使用应用程序图标作为闪屏。在 Qt 应用程序中,可以按原样使用,也可以修改为用作应用程序闪屏,还可以与 Qt 闪屏结合使用。为了保持平台间启动用户体验的一致性,可能需要隐藏 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:theme="@style/splashStartTheme"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

为了启动 Qt 飞屏,我们还需要覆盖 Android ActivityonCreate ,并使用 Intent 来启动 QtActivity。

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

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 intent = new Intent(SplashActivity.this, QtActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);

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

在本例中,主视图只包含一个带有退出按钮的视图。

// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import QtQuick
import QtQuick.Controls

Window {
    visible: true
    title: qsTr("Qt Splash Screen Example")
    color: "#2CDE85"

    Text {
        id: textLabel
        text: "First View"
        anchors.centerIn: parent
    }
    Button {
        id: closeButton
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: textLabel.bottom
        anchors.topMargin: 5
        background: Rectangle {
            color: "#00414A"
            radius: 2
        }
        text: "Close"
        contentItem: Text {
            color: "#FFFFFF"
            text: closeButton.text
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
        onClicked: {
            Qt.callLater(Qt.quit)
        }
    }
}

示例项目 @ 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.