C
Qt Quick Ultralite sprite_animations 示例
演示如何创建精灵动画。
概述
sprite_animations 示例演示了如何根据图像文件中的帧序列创建精灵动画。
![]()
本示例的图像源由 16 个帧组成,每个帧的尺寸为 180x160。

目标平台
- MCIMX93-EVK
- MIMXRT1050
- MIMXRT1060
- MIMXRT1064
- MIMXRT1170
- STM32F769I
- STM32H750B
- RH850 D1M1A
- Infineon TRAVEO T2G
图像格式
Qt Quick Ultralite 允许您使用硬件图像解码器代替默认解码器。AnimatedSprite 和AnimatedSpriteDirectory 这两种 QML 类型均支持精灵动画的注册图像格式。
该示例展示了在STM32F769i、STM32H750b、RH850/D1M1A 评估板以及 Infineon TRAVEO T2G CYT4DN开发板上使用图像解码器的用例。 针对这些开发板,本项目配置了JPEG解码器,并在ImageFiles节点中使用JPEG文件的原始图像数据。图像解码器支持在运行时对图像数据进行解码和渲染。更多详情请参阅Qt Quick Ultralite imagedecoder示例以及《使用JPEG解码器》。
注意:由于 图形驱动程序的限制,目前源 JPEG 图像仅支持 YUV420 子采样模式。 更多信息请参阅《JPEG解码驱动程序示例应用程序用户指南》和《TRAVEO T2G集群系列用户手册》。要将源图像转换为YUV420子采样格式,可以使用ImageMagick转换工具:convert input.jpg -sampling-factor 4:2:0 output.jpg 。
注意:对于 Infineon TRAVEO T2G CYT4DN,请设置必填的 CMake 变量TVII_JPEG_DRIVER_DIR 。该变量应指向 JPEG 解码驱动程序 SDK 的根文件夹。
在 RH850 平台上,请添加以下编译定义来配置 JPEG 驱动程序:
USE_OUTPUT_SPLIT_MODE将Renesas JCUA 硬件配置为分块模式,即以分块形式进行解码。若要使用正常模式(一次性解码完整图像),则可省略此定义。
DECODE_BUFFER_PIXEL_LINES如果启用了
USE_OUTPUT_SPLIT_MODE,则DECODE_BUFFER_PIXEL_LINES将设置每次迭代中要处理的输出缓冲区像素行数。该值必须是16的倍数。DECODE_BUFFER_PIXEL_LINES的默认值为16。CHROMA_SUBSAMPLING设置所用图像的色度子采样格式。可选值如下:
R_JCUA_JPEG_FORMAT_YCBCR420R_JCUA_JPEG_FORMAT_YCBCR411R_JCUA_JPEG_FORMAT_YCBCR422R_JCUA_JPEG_FORMAT_YCBCR444
CHROMA_SUBSAMPLING的默认值为 R_JCUA_JPEG_FORMAT_YCBCR420。注意:目前 无法在运行时确定色度子采样格式,必须在编译时指定。应用程序中使用的所有 JPEG 图像必须采用相同的色度子采样格式保存。
对于其他开发板和平台,该项目使用 PNG 图像文件,并借助Resource Compiler在构建时对图像进行解码。
项目结构
CMake 项目文件
以下 CMake 示例演示了如何在不使用 JPEG 解码器的平台上解码图像:
qul_add_target(sprite_animations
QML_PROJECT mcu_sprite_animations.qmlproject
GENERATE_ENTRYPOINT
)以下 QmlProject 示例展示了如何通过必要的图像资源属性及平台配置来配置 JPEG 解码器:
qul_add_target(sprite_animations
QML_PROJECT mcu_sprite_animations_jpeg.qmlproject
os/${QUL_OS_LOWERCASE}/main.cpp
)QmlProject 文件
QmlProject 文件包含所需的 Qml 和 Image 文件。
// Copyright (C) 2026 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial
import QmlProject 1.3
Project {
mainFile: "sprite_animations.qml"
QmlFiles {
files: [
"ToggleButton.qml"
]
}
ImageFiles {
files: [
"qt-image-sequence.png"
]
MCU.resourceAnimatedSpriteFrameWidth: 180
MCU.resourceAnimatedSpriteFrameHeight: 160
}
}QmlProject 提供了ImageFiles.MCU.resourceAnimatedSpriteFrameWidth、ImageFiles.MCU.resourceAnimatedSpriteFrameHeight 和ImageFiles.MCU.resourceAnimatedSpriteOptimizations,以便您优化图像源。
Resource Compiler会在内部将图像源拆分为 16 个帧,并应用裁剪技术对其进行优化,以提升性能并降低内存占用。这使得系统仅需渲染一系列帧之间发生变化的部分。
应用程序用户界面
sprite_animations.qml 文件定义了AnimatedSprite 类型。它规定了单帧的大小、图像中的帧数以及图像的来源。动画默认处于运行状态。当用户点击图像时,系统会根据当前running 的值来播放或暂停动画。
AnimatedSprite { id: sprite anchors.centerIn: parent source: "qt-image-sequence.png" frameDuration: 80 frameCount: 16 frameWidth: 180 frameHeight: 160 loops: AnimatedSprite.Infinite onFinished: { txtMsg.text = "Finished" } onRunningChanged: { if (sprite.running) { txtMsg.text = "" } } MouseArea { anchors.fill: parent onClicked: { if (sprite.running) { sprite.stop() } else { sprite.start() } } } }
在用户界面的左上角,会显示动画中每个帧的当前帧号及其持续时间(以毫秒为单位)。
Column { id: spriteInfo anchors.top: parent.top anchors.left: parent.left anchors.margins: 16 spacing: 4 Text { id: txtFrameNo text: "frame: " + (sprite.currentFrame + 1) + " / " + sprite.frameCount font.pixelSize: 14 color: "white" } Text { id: txtDuration text: "duration: " + sprite.frameDuration font.pixelSize: 14 color: "white" } Text { id: txtMsg font.pixelSize: 14 text: "" color: "white" } }
应用程序右上角有一个按钮,用于在“loops ”属性值“AnimatedSprite.Infinite”和“1”之间切换。
ToggleButton { id: toggleLoops width: parent.width / 4 height: parent.height / 9 anchors.right: parent.right anchors.top: parent.top anchors.margins: 8 checkedText: "Infinite" uncheckedText: "Once" onCheckedChanged: { if (toggleLoops.checked) { sprite.loops = AnimatedSprite.Infinite } else { sprite.loops = 1 } } }
该 ToggleButton 是一个自定义组件,使用了简单的 QML 可视化类型,例如Rectangle 、MouseArea 、Text 和Row 。
import QtQuick 2.15 Rectangle { id: control color: "white" property bool checked: true readonly property color foregroundColor: "black" readonly property int borderWidth: 1 readonly property alias checkedText: txtChecked.text readonly property alias uncheckedText: txtUnchecked.text Row { x: control.borderWidth y: control.borderWidth spacing: control.borderWidth Rectangle { id: leftPart width: (control.width - control.borderWidth * 3) / 2 height: (control.height - control.borderWidth * 2) color: control.checked? control.color : control.foregroundColor Text { id: txtChecked anchors.centerIn: parent color: control.checked? control.foregroundColor : control.color font.pixelSize: 14 } } Rectangle { id: rightPart width: leftPart.width height: leftPart.height color: control.checked? control.foregroundColor : control.color Text { id: txtUnchecked anchors.centerIn: parent color: control.checked? control.color : control.foregroundColor font.pixelSize: 14 } } } MouseArea { anchors.fill: parent onClicked: control.checked = !control.checked } }
使用 JPEG 解码器
在使用 JPEG 解码器的平台上,您可以使用 QmlProject 的MCU.resourceKeepRawData属性,将 JPEG 文件打包为原始图像数据。您还可以定义MCU.Config.maxResourceCacheSize,以确保运行时有足够的空间来存储解码后的图像数据。
// Copyright (C) 2026 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial
import QmlProject 1.3
Project {
MCU.Config {
// enough size to contain one decoded image
maxResourceCacheSize: 153600
}
QmlFiles {
files: [
"ToggleButton.qml",
"sprite_animations_jpeg.qml"
]
}
ImageFiles {
files: [
"qt-image-sequence/00.jpg",
"qt-image-sequence/01.jpg",
"qt-image-sequence/02.jpg",
"qt-image-sequence/03.jpg",
"qt-image-sequence/04.jpg",
"qt-image-sequence/05.jpg",
"qt-image-sequence/06.jpg",
"qt-image-sequence/07.jpg",
"qt-image-sequence/08.jpg",
"qt-image-sequence/09.jpg",
"qt-image-sequence/10.jpg",
"qt-image-sequence/11.jpg",
"qt-image-sequence/12.jpg",
"qt-image-sequence/13.jpg",
"qt-image-sequence/14.jpg",
"qt-image-sequence/15.jpg"
]
MCU.resourceAnimatedSprite: true
MCU.resourceKeepRawData: true
}
}该示例使用一系列帧图像并结合AnimatedSpriteDirectory ,以突出硬件解码的优势。这可减少精灵动画的内存占用和CPU负载。
AnimatedSpriteDirectory { id: sprite anchors.centerIn: parent sourcePath: "qt-image-sequence" frameDuration: 80 loops: AnimatedSprite.Infinite readonly property int frameCount: 16 ...
文件:
- sprite_animations/CMakeLists.txt
- sprite_animations/ToggleButton.qml
- sprite_animations/mcu_sprite_animations.qmlproject
- sprite_animations/mcu_sprite_animations_jpeg.qmlproject
- sprite_animations/mcu_sprite_animations_t2g.qmlproject
- sprite_animations/os/baremetal/main.cpp
- sprite_animations/sprite_animations.qml
- sprite_animations/sprite_animations_jpeg.qml
图片:
- sprite_animations/qt-image-sequence.png
- sprite_animations/qt-image-sequence/00.jpg
- sprite_animations/qt-image-sequence/01.jpg
- sprite_animations/qt-image-sequence/02.jpg
- sprite_animations/qt-image-sequence/03.jpg
- sprite_animations/qt-image-sequence/04.jpg
- sprite_animations/qt-image-sequence/05.jpg
- sprite_animations/qt-image-sequence/06.jpg
- sprite_animations/qt-image-sequence/07.jpg
- sprite_animations/qt-image-sequence/08.jpg
- sprite_animations/qt-image-sequence/09.jpg
- sprite_animations/qt-image-sequence/10.jpg
- sprite_animations/qt-image-sequence/11.jpg
- sprite_animations/qt-image-sequence/12.jpg
- sprite_animations/qt-image-sequence/13.jpg
- sprite_animations/qt-image-sequence/14.jpg
- sprite_animations/qt-image-sequence/15.jpg
另请参阅 “资源管理”。
在某些 Qt 许可下可用。
了解更多信息。