当前位置: 首页 > news >正文

QML 动画控制、顺序动画与并行动画

目录

    • 引言
    • 相关阅读
    • 基础属性说明
    • 工程结构
    • 示例代码解析
      • 示例1:手动控制动画(ControlledAnimation.qml)
      • 示例2:顺序动画(SequentialAnimationDemo.qml)
      • 示例3:并行动画(ParallelAnimationDemo.qml)
    • 总结
    • 工程下载

引言

接上文:QML 属性动画、行为动画与预定义动画。本文继续通过QML示例,介绍两种动画(顺序动画SequentialAnimation和并行动画ParallelAnimation)如何使用,并附带完整的示例代码。

相关阅读

  • ParallelAnimation官方文档
  • SequentialAnimation官方文档

基础属性说明

属性类型说明
targetObject动画作用的目标对象
propertystring需要动画化的属性名称
durationint动画持续时间(毫秒)
easing.typeenumeration缓动曲线类型(如Easing.InOutQuad)
runningbool控制动画运行状态

工程结构

qml_animation/
├── CMakeLists.txt
├── Main.qml          # 主界面
├── ControlledAnimation.qml      # 手动控制动画
├── SequentialAnimationDemo.qml  # 顺序动画示例
├── ParallelAnimationDemo.qml    # 并行动画示例
├── images.qrc        # 资源文件
└── main.cpp          # 程序入口

示例代码解析

示例1:手动控制动画(ControlledAnimation.qml)

import QtQuick
import QtQuick.ControlsRectangle {width: 400height: 400AnimatedImage {id: controlImgsource: "qrc:/images/huaji.gif"x: 0y: 150width: 100height: 100}NumberAnimation {id: controlledAnimtarget: controlImgproperty: "x"from: 0to: 300duration: 1500}Row {anchors.bottom: parent.bottomspacing: 10Button {text: "开始"onClicked: controlledAnim.start()}Button {text: "暂停"onClicked: controlledAnim.pause()}Button {text: "恢复"onClicked: controlledAnim.resume()}Button {text: "停止"onClicked: controlledAnim.stop()}Button {text: "重启"onClicked: controlledAnim.restart()}}
}

代码说明

这段代码通过组合使用 QML 的各种元素和控件,实现了对一个表情包移动动画的控制功能,用户可以通过点击不同的按钮来控制表情包的移动动画的开始、暂停、恢复、停止和重启。

其中NumberAnimation用于控制表情包的水平移动。设置了动画的目标对象(target)为 controlImg(即前面定义的表情包动态图),要控制的属性(property)为 “x”(水平坐标)。动画的起始位置(from)为 0,结束位置(to)为 300 像素,动画持续时间(duration)为 1500 毫秒。

运行效果

请添加图片描述


示例2:顺序动画(SequentialAnimationDemo.qml)

import QtQuick
import QtQuick.ControlsRectangle {width: 400height: 400SequentialAnimation {id: seqAnimationrunning: falseNumberAnimation { target: rect1; property: "rotation"; from: 0; to: 315; duration: 500 }NumberAnimation { target: rect2; property: "rotation"; from: 0; to: 315; duration: 500 }NumberAnimation { target: rect3; property: "rotation"; from: 0; to: 315; duration: 500 }}Row {spacing: 20anchors.centerIn: parentRectangle {id: rect1width: 80height: 80color: "pink"}Rectangle {id: rect2width: 80height: 80color: "cyan"}Rectangle {id: rect3width: 80height: 80color: "lime"}}Button {text: "启动动画"anchors.bottom: parent.bottomonClicked: {seqAnimation.start()}}
}

代码说明

这段代码通过组合使用 QML 的各种元素和控件,实现了三个彩色矩形的旋转动画效果,并通过按钮来控制动画的启动。用户点击 “启动动画” 按钮后,三个矩形会按照预先定义的动画序列依次旋转。

SequentialAnimation用于定义一个动画序列,动画会按照添加的顺序依次执行。设置了动画序列的初始状态为不运行(running: false)。
定义了三个NumberAnimation元素,每个动画都控制一个矩形(rect1、rect2、rect3)的旋转属性(rotation),起始角度为 0,结束角度为 315 度,动画持续时间均为 500 毫秒。

运行效果

请添加图片描述


示例3:并行动画(ParallelAnimationDemo.qml)

import QtQuick
import QtQuick.ControlsRectangle {width: 400height: 400ParallelAnimation {id: palAnimationrunning: falseNumberAnimation { target: rect1; property: "rotation"; from: 0; to: 315; duration: 500 }NumberAnimation { target: rect2; property: "rotation"; from: 0; to: 315; duration: 500 }NumberAnimation { target: rect3; property: "rotation"; from: 0; to: 315; duration: 500 }}Row {spacing: 20anchors.centerIn: parentRectangle {id: rect1width: 80height: 80color: "pink"}Rectangle {id: rect2width: 80height: 80color: "cyan"}Rectangle {id: rect3width: 80height: 80color: "lime"}}Button {text: "启动动画"anchors.bottom: parent.bottomonClicked: {palAnimation.start()}}
}

代码说明

运行效果

请添加图片描述


总结

通过本文中的三个示例,进行如下总结:

  • 手动控制动画适合需要精确控制动画生命周期的场景
  • 顺序动画适用于需要分步执行的动画序列
  • 并行动画可提升多个动画元素的协同表现力

工程下载

Gitcode项目地址:GitCode -> QML 动画示例

在这里插入图片描述
在之前的项目上增加了3个示例,实现了一个主界面(main.qml),基于swipeview为容器,展示6个示例。

http://www.xdnf.cn/news/483427.html

相关文章:

  • 25考研经验贴(11408)
  • 智能呼叫系统中的NLP意图理解:核心技术解析与实战
  • 游戏引擎学习第286天:开始解耦实体行为
  • R1 快开门式压力容器操作证备考练习题及答案
  • 2025程序设计天梯赛补题报告
  • 《数字藏品APP开发:解锁高效用户身份认证与KYC流程》
  • xss-labs靶场第11-14关基础详解
  • 2025认证杯数学建模第二阶段A题完整论文(代码齐全):小行星轨迹预测思路
  • MySQL的 JOIN 优化终极指南
  • RAG-MCP:突破大模型工具调用瓶颈,告别Prompt膨胀
  • Android Studio AI插件与Bolt工具实战指南:从零到一打造智能应用
  • PostgreSQL中的全页写
  • 【python编程从入门到到实践】第十章 文件和异常
  • Spring框架(三)
  • 7.重建大师点云处理教程
  • 每周靶点:PCSK9、Siglec15及文献分享
  • python基础语法(三-中)
  • [Java][Leetcode middle] 238. 除自身以外数组的乘积
  • 学习alpha
  • 【基础】Windows开发设置入门4:Windows、Python、Linux和Node.js包管理器的作用和区别(AI整理)
  • go.mod关于go版本异常的处理
  • 数据治理域——数据同步设计
  • HTML 中的 input 标签详解
  • 芯片测试之X-ray测试
  • 算法练习:19.JZ29 顺时针打印矩阵
  • SpringAI-RC1正式发布:移除千帆大模型!
  • handsome主题美化及优化:10.1.0最新版 - 2
  • [Unity]AstarPathfindingProject动态烘焙场景
  • 电脑出故障驱动装不上?试试驱动人生的远程服务支持
  • Vue3项目,子组件默认加载了两次,使用 defineAsyncComponent 引入组件后只加载一次