Qt Quick 示例 - 动画
这是一组 QML 动画示例。

“动画”是一组与动画相关的简短 QML 示例。每个示例都是一个简短的 QML 文件,重点介绍某种特定类型或功能。
有关动画的更多信息,请参阅《Qt Quick 中的重要概念——状态、过渡和动画》。
运行示例
您可以通过以下方式运行示例:
- Qt Creator
打开Welcome 模式,并从Examples 中选择该示例。如需了解更多信息,请参阅Qt Creator :教程:构建与运行。
- Qt Extension for Visual Studio Code
在Command Palette 中运行Qt: Open Qt examples 命令,并从列表中选择该示例。有关更多信息,请参阅Qt Extension for Visual Studio Code :教程:构建和运行。
ColorAnimation
ColorAnimation利用颜色动画,将天空从白天渐变为夜晚。
gradient: Gradient {
GradientStop {
position: 0.0
SequentialAnimation on color {
loops: Animation.Infinite
ColorAnimation { from: "#14148c"; to: "#0E1533"; duration: 5000 }
ColorAnimation { from: "#0E1533"; to: "#14148c"; duration: 5000 }
}
}
GradientStop {
position: 1.0
SequentialAnimation on color {
loops: Animation.Infinite
ColorAnimation { from: "#14aaff"; to: "#437284"; duration: 5000 }
ColorAnimation { from: "#437284"; to: "#14aaff"; duration: 5000 }
}
}
}PropertyAnimation
PropertyAnimation使用数值动画使一个圆上下弹跳。
// Animate the y property. Setting loops to Animation.Infinite makes the
// animation repeat indefinitely, otherwise it would only run once.
SequentialAnimation on y {
loops: Animation.Infinite
// Move from minHeight to maxHeight in 300ms, using the OutExpo easing function
NumberAnimation {
from: smiley.minHeight; to: smiley.maxHeight
easing.type: Easing.OutExpo; duration: 300
}
// Then move back to minHeight in 1 second, using the OutBounce easing function
NumberAnimation {
from: smiley.maxHeight; to: smiley.minHeight
easing.type: Easing.OutBounce; duration: 1000
}
// Then pause for 500ms
PauseAnimation { duration: 500 }
}Animators
Animators利用动画器使图标上下弹跳。
SequentialAnimation {
SequentialAnimation {
ParallelAnimation {
YAnimator {
target: smiley;
from: smiley.minHeight;
to: smiley.maxHeight
easing.type: Easing.OutExpo;
duration: 300
}
ScaleAnimator {
target: shadow
from: 1
to: 0.5
easing.type: Easing.OutExpo;
duration: 300
}
}
ParallelAnimation {
YAnimator {
target: smiley;
from: smiley.maxHeight;
to: smiley.minHeight
easing.type: Easing.OutBounce;
duration: 1000
}
ScaleAnimator {
target: shadow
from: 0.5
to: 1
easing.type: Easing.OutBounce;
duration: 1000
}
}
}
PauseAnimation { duration: 500 }
running: true
loops: Animation.Infinite
}Behaviors
“行为”利用行为将一个矩形移动到您点击的位置。
// Set an 'elastic' behavior on the focusRect's y property.
Behavior on y {
NumberAnimation {
easing.type: Easing.OutElastic
easing.amplitude: 3.0
easing.period: 2.0
duration: 300
}
}抖动文本
“晃动文本”演示了如何使用更复杂的行为,在拖动文本时使其产生动画并晃动。它通过为每个字母分配一个复杂的绑定来实现这一点:
x: follow ? follow.x + follow.width : container.width / 6
y: follow ? follow.y : container.height / 2随后,它利用行为来控制每个字母的运动动画:
Behavior on x { enabled: container.animated; SpringAnimation { spring: 3; damping: 0.3; mass: 1.0 } }
Behavior on y { enabled: container.animated; SpringAnimation { spring: 3; damping: 0.3; mass: 1.0 } }Tv Tennis
Tv Tennis利用复杂的“行为”使球拍追随球体,从而模拟一场永无止境的网球比赛。同样,这里将一个依赖其他值的绑定应用于位置属性,并由“行为”提供动画效果。
缓动曲线
《缓动曲线》展示了Qt Quick 动画中所有可用的缓动曲线。
状态
“状态”演示了项目属性在不同状态之间的变化方式。
它定义了几个状态:
// In state 'middleRight', move the image to middleRightRect
State {
name: "middleRight"
PropertyChanges {
userIcon {
x: middleRightRect.x
y: middleRightRect.y
}
}
},
// In state 'bottomLeft', move the image to bottomLeftRect
State {
name: "bottomLeft"
PropertyChanges {
userIcon {
x: bottomLeftRect.x
y: bottomLeftRect.y
}
}
}过渡
“过渡”基于“状态”示例,通过设置过渡效果来实现属性变化的动画效果:
// Transitions define how the properties change when the item moves between each state
transitions: [
// When transitioning to 'middleRight' move x,y over a duration of 1 second,
// with OutBounce easing function.
Transition {
from: "*"; to: "middleRight"
NumberAnimation { properties: "x,y"; easing.type: Easing.OutBounce; duration: 1000 }
},
// When transitioning to 'bottomLeft' move x,y over a duration of 2 seconds,
// with InOutQuad easing function.
Transition {
from: "*"; to: "bottomLeft"
NumberAnimation { properties: "x,y"; easing.type: Easing.InOutQuad; duration: 2000 }
},
// For any other state changes move x,y linearly over duration of 200ms.
Transition {
NumberAnimation { properties: "x,y"; duration: 200 }
}路径动画
“路径动画”利用PathAnimation ,使图像沿贝塞尔曲线进行动画效果。
PathAnimation {
id: pathAnim
duration: 2000
easing.type: Easing.InQuad
target: box
orientation: PathAnimation.RightFirst
anchorPoint: Qt.point(box.width/2, box.height/2)
path: Path {
startX: 50; startY: 50
PathCubic {
x: window.width - 50
y: window.height - 50
control1X: x; control1Y: 50
control2X: 50; control2Y: y
}
onChanged: canvas.requestPaint()
}
}路径插值器
PathInterpolator同样沿同一贝塞尔曲线对图像进行动画处理,但使用的是PathInterpolator 。
PathInterpolator {
id: motionPath
path: Path {
startX: 50; startY: 50
PathCubic {
x: window.width - 50
y: window.height - 50
control1X: x; control1Y: 50
control2X: 50; control2Y: y
}
onChanged: canvas.requestPaint()
}
SequentialAnimation on progress {
running: true
loops: -1
PauseAnimation { duration: 1000 }
NumberAnimation {
id: progressAnim
running: false
from: 0; to: 1
duration: 2000
easing.type: Easing.InQuad
}
}
}© 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.