动画
Qt Quick 提供了对属性进行动画处理的功能。通过动画处理属性,属性值可以经过一系列中间值,而不是立即变为目标值。要对项目的位置进行动画处理,您可以对控制项目位置的属性(例如 x 和 y)进行动画处理,这样项目的位置就会在到达目标位置的过程中,每帧发生变化。
流畅的用户界面
QML的设计旨在简化流畅用户界面的创建。这类用户界面中的组件会通过动画平滑过渡,而非突然出现、消失或跳跃。Qt Quick 提供了两种简单方法,使UI组件能够通过动画移动,而非瞬间出现在新位置。
状态与过渡
Qt Quick 允许您在State 对象中声明各种UI状态。这些状态由相对于基状态的属性变化组成,是组织UI逻辑的有效方式。过渡是可与项目关联的对象,用于定义当属性因状态变化而改变时,其动画效果应如何呈现。
项目的状态和过渡可通过Item::states 和Item::transitions 属性进行声明。状态声明在项目的states列表属性中,通常是组件的根项目。在同一项目上定义的过渡用于对状态变化进行动画处理。以下是一个示例。
Item {
id: container
width: 320
height: 120
Rectangle {
id: rect
color: "red"
width: 120
height: 120
TapHandler {
onTapped: container.state === '' ? container.state = 'other' : container.state = ''
}
}
states: [
// This adds a second state to the container where the rectangle is farther to the right
State { name: "other"
PropertyChanges {
target: rect
x: 200
}
}
]
transitions: [
// This adds a transition that defaults to applying to all state changes
Transition {
// This applies a default NumberAnimation to any changes a state change makes to x or y properties
NumberAnimation { properties: "x,y" }
}
]
}属性变化的动画效果。
行为(Behaviors)可用于指定属性在发生变化时应采用的动画效果。该动画将应用于所有属性变化,无论其来源如何。以下示例展示了如何利用行为实现按钮在屏幕上移动的动画效果。
Item {
width: 320
height: 120
Rectangle {
color: "green"
width: 120
height: 120
// This is the behavior, and it applies a NumberAnimation to any attempt to set the x property
Behavior on x {
NumberAnimation {
//This specifies how long the animation takes
duration: 600
//This selects an easing curve to interpolate with, the default is Easing.Linear
easing.type: Easing.OutBounce
}
}
TapHandler {
onTapped: parent.x == 0 ? parent.x = 200 : parent.x = 0
}
}
}其他动画
并非所有动画都必须与特定的属性或状态绑定。您还可以更通用地创建动画,并在动画内部指定目标项和属性。以下是一些实现此功能的不同示例:
Item {
width: 320
height: 120
Rectangle {
color: "blue"
width: 120
height: 120
// By setting this SequentialAnimation on x, it and animations within it will automatically animate
// the x property of this element
SequentialAnimation on x {
id: xAnim
// Animations on properties start running by default
running: false
loops: Animation.Infinite // The animation is set to loop indefinitely
NumberAnimation { from: 0; to: 200; duration: 500; easing.type: Easing.InOutQuad }
NumberAnimation { from: 200; to: 0; duration: 500; easing.type: Easing.InOutQuad }
PauseAnimation { duration: 250 } // This puts a bit of time between the loop
}
TapHandler {
// The animation starts running when you click within the rectangle
onTapped: xAnim.running = true
}
}
}
Item {
width: 320
height: 120
Rectangle {
id: rectangle
color: "yellow"
width: 120
height: 120
TapHandler {
// The animation starts running when you click within the rectangle
onTapped: anim.running = true;
}
}
// This animation specifically targets the Rectangle's properties to animate
SequentialAnimation {
id: anim
// Animations on their own are not running by default
// The default number of loops is one, restart the animation to see it again
NumberAnimation { target: rectangle; property: "x"; from: 0; to: 200; duration: 500 }
NumberAnimation { target: rectangle; property: "x"; from: 200; to: 0; duration: 500 }
}
}
有关动画的更多信息,请参阅《Qt Quick 中的重要概念——状态、过渡和动画》页面。
© 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.