qt创建surfaceview
时间: 2023-10-29 12:04:43 浏览: 233
SurfaceView-Sample-master.zip_android_surfaceview
在Qt中创建SurfaceView需要使用AndroidJNI,具体步骤如下:
1. 在.pro文件中添加以下内容:
```
android {
QT += androidextras
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
ANDROID_EXTRA_LIBS = $$PWD/android/libs
ANDROID_PERMISSIONS = android.permission.CAMERA,android.permission.RECORD_AUDIO
# Define Android ABI-specific folders
ANDROID_EXTRA_LIBS += \
$$PWD/android/libs/armeabi-v7a \
$$PWD/android/libs/arm64-v8a \
$$PWD/android/libs/x86 \
$$PWD/android/libs/x86_64
# Define your Java source files
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
ANDROID_PACKAGE_BUILD_DIR = $$PWD/android-build
ANDROID_DEPLOYMENT_SETTINGS += \
android-extra-libs.json \
android-extra-libs-armeabi-v7a.json \
android-extra-libs-arm64-v8a.json \
android-extra-libs-x86.json \
android-extra-libs-x86_64.json
}
```
2. 在android文件夹下创建AndroidManifest.xml文件,并添加以下内容:
```
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<application
android:name="android.app.Application"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="org.qtproject.qt5.android.bindings.QtActivity"
android:label="@string/app_name"
android:configChanges="orientation|screenSize|keyboardHidden">
<meta-data android:name="android.app.lib_name" android:value="Qt5Core"/>
<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"/>
<meta-data android:name="android.app.bundle_local_qt_libs" android:value="armeabi-v7a:Qt5AndroidExtras_armeabi-v7a,arm64-v8a:Qt5AndroidExtras_arm64-v8a,x86:Qt5AndroidExtras_x86,x86_64:Qt5AndroidExtras_x86_64"/>
<meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
<meta-data android:name="android.app.qt_cflags" android:value="-fstack-protector-strong -DANDROID -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -O2 -g -DNDEBUG -std=c++11 -Werror -Wall -Wno-unused-value -Wno-unused-function -Wno-unused-variable -Wno-deprecated-declarations -D__ANDROID_API__=19"/>
<meta-data android:name="android.app.qt_cppflags" android:value="-fstack-protector-strong -DANDROID -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -O2 -g -DNDEBUG -std=c++11 -Werror -Wall -Wno-unused-value -Wno-unused-function -Wno-unused-variable -Wno-deprecated-declarations -D__ANDROID_API__=19"/>
<meta-data android:name="android.app.qt_ldflags" android:value="-Wl,-rpath=/data/data/org.qtproject.example.Surfacetest/files/qt/lib -Wl,-rpath-link,/data/data/org.qtproject.example.Surfacetest/files/qt/lib -Wl,-rpath-link,$ORIGIN/lib -L/data/data/org.qtproject.example.Surfacetest/files/qt/lib -lQt5AndroidExtras"/>
<meta-data android:name="android.app.qt_static_cppflags" android:value=""/>
<meta-data android:name="android.app.qt_static_ldflags" android:value=""/>
<meta-data android:name="android.app.qt_tr_sources_resource_id" android:resource="@array/qt_tr_sources"/>
<meta-data android:name="android.app.qt_translation" android:resource="@array/qt_translation"/>
<meta-data android:name="android.architecture" android:value="armeabi-v7a"/>
</activity>
<activity
android:name="org.qtproject.qt5.android.bindings.QtService"
android:process=":qt"/>
<meta-data android:name="android.app.lib_name" android:value="Qt5AndroidExtras"/>
</application>
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="26"/>
```
3. 在android文件夹下创建build.gradle文件,并添加以下内容:
```
dependencies {
compile files('libs/Qt5AndroidExtras.jar')
compile files('libs/libQt5AndroidExtras_armeabi-v7a.so')
compile files('libs/libQt5AndroidExtras_arm64-v8a.so')
compile files('libs/libQt5AndroidExtras_x86.so')
compile files('libs/libQt5AndroidExtras_x86_64.so')
}
```
4. 在C++代码中使用AndroidJNI来创建SurfaceView,以下是一个示例:
```
void MyWidget::createSurfaceView()
{
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
if (activity.isValid()) {
QAndroidJniObject viewGroup = QAndroidJniObject::callStaticObjectMethod("android/view/View", "getRootView", "()Landroid/view/View;");
if (viewGroup.isValid()) {
QAndroidJniObject surfaceView = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "createSurfaceView", "(Landroid/content/Context;)Landroid/view/SurfaceView;", activity.object());
if (surfaceView.isValid()) {
surfaceView.callMethod<void>("setZOrderMediaOverlay", "(Z)V", true);
surfaceView.callMethod<void>("setZOrderOnTop", "(Z)V", true);
surfaceView.callMethod<void>("setBackgroundColor", "(I)V", 0x00000000);
QAndroidJniObject layoutParams = QAndroidJniObject("android/view/ViewGroup$LayoutParams", "(II)V", 0, 0);
viewGroup.callMethod<void>("addView", "(Landroid/view/View;Landroid/view/ViewGroup$LayoutParams;)V", surfaceView.object(), layoutParams.object());
}
}
}
}
```
在createSurfaceView方法中,我们使用了QAndroidJniObject来获取Android Activity和ViewGroup,然后使用QtNative类中的createSurfaceView静态方法来创建SurfaceView,并将其添加到ViewGroup中。
需要注意的是,SurfaceView需要设置setZOrderMediaOverlay和setZOrderOnTop方法为true,才能在Qt应用中正常显示。此外,为了避免SurfaceView遮挡其他控件,我们还需要将其背景颜色设置为透明。
阅读全文