LottieAnimation QML Type
Qt 的 Lottie 播放器更多
| Import Statement: | import Qt.labs.lottieqt 1.0 |
| Inherits: |
属性
- autoPlay : bool
- direction : enumeration
- endFrame : int
- frameRate : int
- loops : int
- quality : enumeration
- source : url
- startFrame : int
- status : enumeration
信号
- finished()
方法
- double getDuration(bool inFrames)
- void gotoAndPlay(int frame)
- bool gotoAndPlay(string frameMarker)
- void gotoAndStop(int frame)
- bool gotoAndStop(string frameMarker)
- void pause()
- void play()
- void start()
- void stop()
- void togglePause()
详细说明
LottieAnimation 类型显示 Lottie 格式文件。
LottieAnimation 用于加载和渲染从 Adobe After Effects 导出的 Lottie 文件。目前,只支持完整 Lottie 规范的子集。最显著的偏差包括
- 仅支持形状层
- 仅支持时间线的整帧模式(实际帧数和时间四舍五入为最接近的整数)
- 不支持表达式
有关偏差的完整列表,请参阅限制部分。
使用示例
以下示例展示了 LottieAnimation 类型的简单用法
LottieAnimation { loops: 2 quality: LottieAnimation.MediumQuality source: "animation.json" autoPlay: false onStatusChanged: { if (status === LottieAnimation.Ready) { // any acvities needed before // playing starts go here gotoAndPlay(startFrame); } } onFinished: { console.log("Finished playing") } }
注意: 改变元素的宽度或高度不会改变其中动画的大小。此外,无法对齐LottieAnimation 元素内部的内容。要做到这一点,请将动画置于Item 等元素内。
渲染性能
在内部,渲染帧数据会被缓存以提高性能。你可以通过设置 QLOTTIE_RENDER_CACHE_SIZE 环境变量(默认值为 2)来控制内存使用量。
你可以通过开启两种日志类别来监控渲染性能:
qt.lottieqt.lottie.render- 提供动画渲染过程的信息qt.lottieqt.lottie.render.thread- 提供渲染过程如何进行的信息。
具体来说,你可以监控帧缓存是否持续满载,或者渲染过程是否需要等待帧准备就绪。第一种情况意味着动画过于复杂,渲染跟不上节奏。试着让动画更简单,或优化 QML 场景。
属性文档
autoPlay : bool
定义播放器是否会在加载动画文件后自动开始播放动画。
默认值为true 。
direction : enumeration
此属性表示渲染的方向。
| 常数 | 描述 |
|---|---|
LottieAnimation.Forward | 正向(默认) |
LottieAnimation.Reverse | 反向 |
endFrame : int [read-only]
动画结束时的帧数。该值在动画加载并准备播放后可用。
frameRate : int
此属性保存 Lottie 动画的帧频值。
frameRate 该值在加载资产后才会改变。在此之前更改帧频不会产生影响,因为资产中定义的值会覆盖该值。要更改帧频,可以写入
LottieAnimation {
source: "animation.json"
onStatusChanged: {
if (status === LottieAnimation.Ready)
frameRate = 60;
}loops : int
此属性表示播放器重复播放的次数。LottieAnimation.Infinite 表示播放器连续重复播放动画。
默认值为1 。
quality : enumeration
用于指定 lottie 播放器的渲染质量。如果选择LowQuality ,渲染将在帧缓存对象中进行,而如果选择其他选项,渲染将在QImage 上进行(进而在屏幕上渲染)。
| 常数 | 说明 |
|---|---|
LottieAnimation.LowQuality | 不使用抗锯齿或平滑像素图变换算法 |
LottieAnimation.MediumQuality | 使用平滑像素图变换算法,但不使用抗锯齿(默认值) |
LottieAnimation.HighQuality | 同时使用抗锯齿和平滑像素图变换算法 |
source : url
LottieAnimation 播放的 Lottie 资产的来源。
LottieAnimation 可以处理 Qt 支持的任何 URL 方案。URL 可以是绝对的,也可以是相对于组件 URL 的。
设置源属性可异步加载动画。要监控加载进度,请连接status 更改信号。
startFrame : int [read-only]
动画开始的帧数。该值在动画加载并准备播放后可用。
status : enumeration
该属性用于保存LottieAnimation 元素的当前状态。
| 常量 | 说明 |
|---|---|
LottieAnimation.Null | 未定义源时使用的初始值(默认值) |
LottieAnimation.Loading | 播放器正在加载 Lottie 文件 |
LottieAnimation.Ready | 加载已成功完成,播放器已准备好播放动画 |
LottieAnimation.Error | 加载动画时发生错误 |
例如,你可以实现onStatusChanged 信号处理器来监控加载动画的进度,具体如下:
LottieAnimation {
source: "animation.json"
autoPlay: false
onStatusChanged: {
if (status === LottieAnimation.Ready)
start();
}信号文档
finished()
播放器结束播放时会发出该信号。如果是循环播放,则在最后一个循环结束时发出该信号。
注: 相应的处理程序是onFinished 。
方法文档
double getDuration(bool inFrames)
返回当前播放资产的持续时间。
如果给定的inFrames 是true ,返回值就是以帧数为单位的持续时间。否则,将以秒为单位返回持续时间。
void gotoAndPlay(int frame)
播放给定frame 中的资产。
bool gotoAndPlay(string frameMarker)
播放具有给定frameMarker 标记的帧中的资产。如果找到 frameMarker,则返回true ,否则返回false 。
void gotoAndStop(int frame)
将播放头移动到给定的frame 并停止。
bool gotoAndStop(string frameMarker)
将播放头移动到给定的标记处并停止。如果找到frameMarker ,则返回true ,否则返回false 。
void pause()
暂停播放。
void play()
从当前位置开始或继续播放。
void start()
从头开始播放动画。
void stop()
停止播放并返回startFrame 。
void togglePause()
在播放状态和暂停状态之间切换播放器的状态。
© 2026 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.