感言
时隔1年多,又开始搞Qt Android开发了,再次回头看我之前的记录,发现很多问题都被官方友好的解决了,而我的知识面还停留在5.9那个阶段,所以再次搞新版的Qt安卓既陌生又熟悉。咳咳,先感叹感叹一下,好了,回归正题。
首先,这次我开发的qt版本是最新版本Qt5.12.2,使用的是Android_for_arm64_v8a版本,这个版本是新出的,我在5.9的时候还没见过,网上说法是支持64位,之前的armeabi_v7a是支持32位的,64位是向下支持的,所以也是尝鲜心理,就采用最新的模式(armeabi_v7a模式我也跑过,一样没问题的哦!!!)
问题
今天要说的问题还是我之前说过的一个老问题,就是启动界面的问题,不过这次Qt官方有了很好的解决,我之前的 Qt for android 在安卓上面运行app启动闪黑屏的解决方法只是暂时解决那个问题,缺陷还是非常大的,这次找到的方案可以说是完美解决,可以相当原生开发一样完美了。
好了,知道各位已经等不及了。先列出我已知的2种解决方案(可能还有第3种,第4种。。。靠各种大大自己去发掘更好的方案了):
解决方案
解决方案一:
使用透明主题
点击项目 -> 在 构建设置 里面找到 Build Android APK 栏目,点击 create templates 创建一个 AndroidManifest.xml(刚接触这个新版本的时候我都忘记这个按钮在哪里了,让我废了好大时间才熟悉找到),贴图如下:
xml文件如下:
<?xml version="1.0"?>
<manifest package="org.qtproject.example" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
<application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="-- %%INSERT_APP_NAME%% --">
<activity
android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation"
android:name="org.qtproject.qt5.android.bindings.QtActivity"
android:label="-- %%INSERT_APP_NAME%% --"
android:screenOrientation="unspecified"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
<!-- 添加上面一条即可,只是把黑屏设置成透明的而已,启动有点慢,不过也是很完美的哦!亲测 -->
<!-- @android:style/Theme.Translucent -->
<!-- @android:style/Theme.Translucent.NoTitleBar -->
<!-- @android:style/Theme.Translucent.NoTitleBar.Fullscreen -->
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Application arguments -->
<!-- meta-data android:name="android.app.arguments" android:value="arg1 arg2 arg3"/ -->
<!-- Application arguments -->
<meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/>
<meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/>
<meta-data android:name="android.app.repository" android:value="default"/>
<meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/>
<meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/>
<!-- Deploy Qt libs as part of package -->
<meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/>
<meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
<meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/>
<!-- Run with local libs -->
<meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/>
<meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
<meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/>
<meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/>
<meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/>
<!-- Messages maps -->
<meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/>
<meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/>
<meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
<!-- Messages maps -->
<!-- Splash screen -->
<!-- meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/logo"/ -->
<!-- meta-data android:name="android.app.splash_screen_sticky" android:value="true"/ -->
<!-- Splash screen -->
<!-- Background running -->
<!-- Warning: changing this value to true may cause unexpected crashes if the
application still try to draw after
"applicationStateChanged(Qt::ApplicationSuspended)"
signal is sent! -->
<meta-data android:name="android.app.background_running" android:value="false"/>
<!-- Background running -->
<!-- auto screen scale factor -->
<meta-data android:name="android.app.auto_screen_scale_factor" android:value="false"/>
<!-- auto screen scale factor -->
<!-- extract android style -->
<!-- available android:values :
* full - useful QWidget & Quick Controls 1 apps
* minimal - useful for Quick Controls 2 apps, it is much faster than "full"
* none - useful for apps that don't use any of the above Qt modules
-->
<meta-data android:name="android.app.extract_android_style" android:value="full"/>
<!-- extract android style -->
</activity>
<!-- For adding service(s) please check: https://wiki.qt.io/AndroidServices -->
</application>
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="16"/>
<supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
<!-- The following comment will be replaced upon deployment with default permissions based on the dependencies of the application.
Remove the comment if you do not require these default permissions. -->
<!-- %%INSERT_PERMISSIONS -->
<!-- The following comment will be replaced upon deployment with default features based on the dependencies of the application.
Remove the comment if you do not require these default features. -->
<!-- %%INSERT_FEATURES -->
</manifest>
亲测可用还可靠,使用效果很不错,为了达到更好的效果,各位客官可以看我启动页延时关闭博客。
解决方案二:
使用图片替换主题
在res/values文件目录下新建一个 style.xml 文件,文件内容如下:
<?xml version='1.0' encoding='utf-8'?>
<resources>
<style name="Theme.AppStartLoad" parent="android:Theme">
<item name="android:windowBackground">@drawable/logo</item>
<!-- @drawable/logo 确保 android\res\drawable-hdpi\logo.png 存在 -->
<item name="android:windowNoTitle">true</item>
</style>
<style name="Theme.AppStartLoadTranslucent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
然后在 AndroidManifest.xml 中应用上面定义的两个主题,添加的位置如下:
<application android:theme = "@style/Theme.AppStartLoadTranslucent">
<activity android:theme="@style/Theme.AppStartLoad">
<!-- Splash screen -->
<!-- <meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/logo"/> -->
<!-- <meta-data android:name="android.app.splash_screen_sticky" android:value="true"/> -->
<!-- Splash screen -->
//这上面的是添加启动图片
如图所示:文件太长,分两次截图,自行组合查看
这个方案中,如下图:
这一块可以去掉或者使用方案一,不然的话启动很完美就是标题栏会缩一下,导致很突兀。
遗留问题
这里设置只是为了解决上面的问题,可是启动页面还是会很快或者一闪就进入主页面了,还未达到更好的效果,为了更好的配合启动页面,请参考我下面这篇博客Qt for Android Splash启动页延时关闭