动画是提升用户界面交互体验的核心技术。在QML中,动画系统强大而灵活,允许开发者以声明式的方式创建平滑、生动的视觉效果。本章将深入讲解QML中几种核心的动画类型和行为控制机制,帮助你掌握界面动态效果的实现。
它是QML动画的“万能工具”,可以对任何属性值的变化进行平滑过渡,不管是位置、大小还是透明度,只要是对象属性,都能通过它实现动态变化。
常用属性包括:target指定动画目标对象,property定义要动画的属性名,to/from设置目标值与起始值,duration控制动画持续时长(单位毫秒),easing.type选择缓动曲线让动画更自然。
示例代码:
import QtQuick 2.15
Rectangle {
width: 100; height: 100
color: "lightblue"
PropertyAnimation on x {
to: 300
duration: 1000
running: true
}
}
作为PropertyAnimation的特化版本,它专门针对数值类型属性优化,效率更高,支持整数和浮点数,是处理x/y坐标、旋转角度、透明度等数值属性的首选。
示例代码:
Rectangle {
id: rect
width: 50; height: 50
color: "orange"
MouseArea {
anchors.fill: parent
onClicked: numAnim.start()
}
NumberAnimation {
id: numAnim
target: rect
property: "rotation"
from: 0
to: 360
duration: 2000
}
}
专门用于颜色属性的平滑过渡,支持所有颜色格式(如“red”、“#FF0000”、“rgb(255,0,0)”),能让颜色变化像呼吸一样自然,常用来做按钮 hover 效果或主题切换动画。
示例代码:
Rectangle {
width: 200; height: 200
color: "green"
ColorAnimation on color {
to: "yellow"
duration: 3000
loops: Animation.Infinite
running: true
}
}
Behavior元素相当于给属性装了个“自动动画开关”,当属性值发生变化时,会自动应用指定的动画效果,无需显式调用动画,既简化代码,又让动画逻辑与属性绑定,提升可维护性。
示例代码:
Rectangle {
width: 100; height: 100
color: "skyblue"
Behavior on x {
NumberAnimation { duration: 500; easing.type: Easing.OutBounce }
}
Behavior on y {
NumberAnimation { duration: 500; easing.type: Easing.OutBack }
}
MouseArea {
anchors.fill: parent
onClicked: {
parent.x = mouse.x - parent.width/2
parent.y = mouse.y - parent.height/2
}
}
}
按顺序逐个执行子动画,就像排队完成任务,适合制作有先后逻辑的动画序列,比如先移动到指定位置,再执行旋转效果。
示例代码:
Rectangle {
id: seqRect
width: 80; height: 80
color: "red"
SequentialAnimation {
id: seqAnim
running: true
NumberAnimation {
target: seqRect
property: "x"
to: 400
duration: 1000
}
NumberAnimation {
target: seqRect
property: "y"
to: 300
duration: 1000
}
RotationAnimation {
target: seqRect
to: 360
duration: 1000
}
}
}
同时执行所有子动画,多个动画效果同步发力,比如让矩形边移动边变色,轻松打造丰富的复合视觉效果。
示例代码:
Rectangle {
id: parRect
width: 80; height: 80
color: "purple"
ParallelAnimation {
id: parAnim
running: true
NumberAnimation {
target: parRect
property: "x"
to: 400
duration: 2000
}
NumberAnimation {
target: parRect
property: "y"
to: 300
duration: 2000
}
ColorAnimation {
target: parRect
property: "color"
to: "cyan"
duration: 2000
}
}
}
不同动画类型各有侧重,选择时可参考以下方向:
下面是一个结合多种动画类型的完整示例,点击界面任意位置,矩形会同时移动到目标点、随机变色,还会完成旋转动画,弹性缓动让效果更贴近真实物理运动:
import QtQuick 2.15
Item {
width: 600; height: 400
Rectangle {
id: animatedBox
width: 100; height: 100
color: "#FF5722"
radius: 10
// 位置变化自动触发弹性动画
Behavior on x {
NumberAnimation {
duration: 800
easing.type: Easing.OutElastic
easing.amplitude: 2.0
}
}
Behavior on y {
NumberAnimation {
duration: 800
easing.type: Easing.OutBack
}
}
// 颜色变化自动触发过渡动画
Behavior on color {
ColorAnimation { duration: 500 }
}
// 点击触发的旋转顺序动画
SequentialAnimation on rotation {
id: spinAnim
loops: 1
running: false
NumberAnimation {
from: 0
to: 360
duration: 1000
easing.type: Easing.InOutQuad
}
PauseAnimation { duration: 300 }
NumberAnimation {
from: 360
to: 0
duration: 500
}
}
}
MouseArea {
anchors.fill: parent
onClicked: {
// 并行动画:移动+变色同步执行
ParallelAnimation {
NumberAnimation {
target: animatedBox
property: "x"
to: mouse.x - animatedBox.width/2
duration: 1000
}
NumberAnimation {
target: animatedBox
property: "y"
to: mouse.y - animatedBox.height/2
duration: 1000
}
ColorAnimation {
target: animatedBox
property: "color"
to: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
duration: 1000
}
}.start()
// 启动旋转动画
spinAnim.start()
}
}
}