本页内容

SmoothedAnimation QML Type

允许属性平滑地跟踪某个值。更多...

Import Statement: import QtQuick
Inherits:

NumberAnimation

属性

详细说明

SmoothedAnimation 通过使用“渐入/渐出”四元缓动曲线,将属性的值动画化至设定的目标值。当目标值发生变化时,用于在旧目标值和新目标值之间进行动画过渡的缓动曲线会被平滑地拼接在一起,从而生成一个平滑的运动轨迹,使对象在保持当前速度的同时过渡到新目标值。

以下示例展示了其中一个Rectangle 如何通过SmoothedAnimation跟踪另一个 的位置。绿色矩形的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 来配置 SmoothedAnimation。如果同时指定了velocity 和duration ,则每次目标值发生变化时,系统会选择能实现最快动画效果的那一个。

例如,从 0 动画到 800:若设置速度为 200,则需 4 秒;若设置持续时间为 8000,则需 8 秒;若同时设置速度为 200 且持续时间为 8000,则需 4 秒。 如果将速度设置为 200,从 0 动画到 20000 需要 10 秒;如果将持续时间设置为 8000,则需要 8 秒;如果同时将速度和持续时间都设置为 200 和 8000,则同样需要 8 秒。

SmoothedAnimation 的默认速度为 200 单位/秒。请注意,如果动画值的范围较小,则需要适当调整速度。例如,某个项的透明度范围为 0 - 1.0。 为了在此范围内实现平滑动画,需要将速度设置为 0.5 单位/秒等值。以 0.5 的速度从 0 动画到 1.0 将需要 2000 毫秒才能完成。

与其他动画类型一样,SmoothedAnimation 可以通过多种方式应用,包括过渡、行为和属性值来源。 Qt Quick 文档中的“动画与过渡”部分展示了创建动画的多种方法。

另请参阅 SpringAnimation 、NumberAnimation 、《Qt Quick 》中的“动画与过渡”以及Qt Quick Examples - Animation。

属性文档

duration : int

该属性存储了跟踪源时使用的动画持续时间(单位为毫秒)。

将其设置为 -1(默认值)将禁用该持续时间值。

如果速度值和持续时间值均已启用,则动画将采用二者中持续时间较短的那一个。

maximumEasingTime : int

该属性指定跟随过程中任何“缓动”效果应持续的最大时间(单位:毫秒)。设置此属性后,速度将在一段时间后“趋于平稳”。设置负值将恢复为在整个动画时长内进行缓动的正常模式。

默认值为 -1。

reversingMode : enumeration

设置当动画方向发生反转时,SmoothedAnimation 的行为方式。

可能的取值为:

Constant描述
SmoothedAnimation.Eased(默认)动画将平滑减速,然后反向
SmoothedAnimation.Immediate动画将立即开始沿反向加速,初始速度为 0
SmoothedAnimation.Sync该属性将立即设置为目标值

velocity : real

该属性存储了跟踪“to”值时允许的平均速度。

SmoothedAnimation 的默认速度为200单位/秒。

将此值设置为 -1 将禁用该速度值。

如果速度值和持续时间值均已启用,则动画将采用二者中持续时间较短的那一个。

© 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.