SmoothedAnimation QML Type
允许一个属性平滑跟踪一个值。更多
Import Statement: | import QtQuick |
Inherits: |
属性
- duration : int
- maximumEasingTime : int
- reversingMode : enumeration
- velocity : real
详细说明
平滑动画(SmoothedAnimation)使用缓入/缓出四边形缓和曲线将属性值动画化为设定的目标值。当目标值发生变化时,用于在新旧目标值之间制作动画的缓和曲线会平滑地拼接在一起,从而在保持当前速度的情况下平滑移动到新的目标值。
下面的示例显示了一个Rectangle 使用平滑动画跟踪另一个 的位置。绿色矩形的x
和y
值与红色矩形的值绑定。每当这些值发生变化时,绿色矩形就会以平滑动画的方式移动到新的位置:
import QtQuick Rectangle { width: 800; height: 600 color: "blue" Rectangle { width: 60; height: 60 x: rect1.x - 5; y: rect1.y - 5 color: "green" Behavior on x { SmoothedAnimation { velocity: 200 } } Behavior on y { SmoothedAnimation { velocity: 200 } } } Rectangle { id: rect1 width: 50; height: 50 color: "red" } focus: true Keys.onRightPressed: rect1.x = rect1.x + 100 Keys.onLeftPressed: rect1.x = rect1.x - 100 Keys.onUpPressed: rect1.y = rect1.y - 100 Keys.onDownPressed: rect1.y = rect1.y + 100 }
平滑动画可通过设置动画发生的velocity 或动画应采取的duration 进行配置。如果同时指定了velocity 和duration ,则在目标值每次变化时,都会选择动画速度最快的那个。
例如,如果速度设置为 200,从 0 到 800 的动画需要 4 秒;如果持续时间设置为 8000,则需要 8 秒;如果速度和持续时间都设置为 200 和 8000,则需要 4 秒。如果速度设置为 200,从 0 到 20000 的动画将耗时 10 秒;如果持续时间设置为 8000,将耗时 8 秒;如果速度设置为 200,持续时间设置为 8000,将耗时 8 秒。
平滑动画的默认速度为 200 单位/秒。请注意,如果动画值的范围较小,则需要适当调整速度。例如,项目的不透明度范围为 0 - 1.0。为了在此范围内实现平滑的动画效果,需要将速度设置为 0.5 单位/秒。速度为 0.5 时,从 0 到 1.0 的动画需要 2000 毫秒才能完成。
与其他动画类型一样,平滑动画可以多种方式应用,包括过渡、行为和属性值源。 Qt Quick 文档 中的动画和过渡显示了创建动画的多种方法。
另请参阅 SpringAnimation,NumberAnimation, Qt Quick 中的动画和过渡, 以及Qt Quick 示例 - 动画。
属性文档
duration : int |
该属性用于保存跟踪源时使用的动画持续时间(以毫秒为单位)。
将其设置为-1(默认值)将禁用持续时间值。
如果同时启用了速度值和持续时间值,则动画将使用持续时间较短的值。
maximumEasingTime : int |
以毫秒为单位,该属性指定了跟随过程中任何 "缓和 "所需的最长时间。设置该属性会使速度在一段时间后 "平缓 "下来。设置负值则会在整个动画持续时间内恢复到正常的缓和模式。
默认值为-1。
reversingMode : enumeration |
设置SmoothedAnimation 在动画方向反转时的行为方式。
可能的值有
常量 | 说明 |
---|---|
SmoothedAnimation.Eased | (默认值)动画将平稳减速,然后反转方向 |
SmoothedAnimation.Immediate | 动画将立即开始反向加速,速度从 0 开始 |
SmoothedAnimation.Sync | 该属性会立即设置为目标值 |
velocity : real |
该属性用于保存跟踪 "到 "值时允许的平均速度。
SmoothedAnimation 的默认速度为 200 单位/秒。
将该属性设置为-1 时,将禁用速度值。
如果同时启用了速度值和持续时间值,则动画将使用持续时间较短的值。
© 2025 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.