AndroidでQt Quick を使ってスプラッシュスクリーンを実装する
Android でQt Quick View を使ってスプラッシュスクリーンを設定する方法を紹介します。
この簡単な例では、QML ビューを Qt スプラッシュスクリーンとして使用する方法を示します。追加のトランジションは使用していません。この例では、Android 12 で導入されたデフォルトのスプラッシュスクリーンを非表示にしています。アプリケーションが起動すると、スプラッシュスクリーンが2秒間表示され、その後、メインビューがトランジションなしで表示されます。
この例は、スプラッシュ・スクリーンを示す2つの同様の例のうちの1つです。1つはQMLビューを使用し、もう1つは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"
タイマーはタイムアウト時にexit関数を呼び出し、スプラッシュスクリーンからメインビューに切り替えます。可能なアウトアニメーションはここで開始されます。
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ビューの向きがその下にあるビューから取得されるという事実が原因です。通常、これはApplication Drawerで、向きを変更することはありません。そして、アクティビティ・ビューが設定された後にテーマを変更することはできないため、結局、アプリケーションは1つの向きで止まってしまいます。
回避策として、Androidのスプラッシュ画面用に半透明のテーマを持つ捨て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>
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が1つだけ含まれています。
© 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.