PropertyAnimation QML Type

动画显示属性值的变化。更多

Import Statement: import QtQuick
Inherits:

Animation

Inherited By:

ColorAnimation, NumberAnimation, RotationAnimation, and Vector3dAnimation

属性

详细说明

PropertyAnimation 为属性值的变化提供了一种动画方式。

它可用于以多种方式定义动画:

  • Transition

    例如,使用InOutQuad 缓和曲线,为任何因状态变化而改变了xy 属性的对象制作动画:

    Rectangle {
        id: rect
        width: 100; height: 100
        color: "red"
    
        states: State {
            name: "moved"
            PropertyChanges { target: rect; x: 50 }
        }
    
        transitions: Transition {
            PropertyAnimation { properties: "x,y"; easing.type: Easing.InOutQuad }
        }
    }
  • Behavior

    例如,将矩形x 属性的所有变化制作成动画:

    Rectangle {
        width: 100; height: 100
        color: "red"
    
        Behavior on x { PropertyAnimation {} }
    
        MouseArea { anchors.fill: parent; onClicked: parent.x = 50 }
    }
  • 作为属性值源

    例如,将矩形的x 属性重复制成动画:

    Rectangle {
        width: 100; height: 100
        color: "red"
    
        SequentialAnimation on x {
            loops: Animation.Infinite
            PropertyAnimation { to: 50 }
            PropertyAnimation { to: 0 }
        }
    }
  • 在信号处理器中

    例如,在点击时淡出theObject

    MouseArea {
        anchors.fill: theObject
        onClicked: PropertyAnimation { target: theObject; property: "opacity"; to: 0 }
    }
  • 独立

    例如,在 500ms 内将rectwidth 属性从当前宽度调整为 30:

    Rectangle {
        id: theRect
        width: 100; height: 100
        color: "red"
    
        // this is a standalone animation, it's not running by default
        PropertyAnimation { id: animation;
                            target: theRect;
                            property: "width";
                            to: 30;
                            duration: 500 }
    
        MouseArea { anchors.fill: parent; onClicked: animation.running = true }
    }

根据动画的使用方式,通常使用的属性集将有所不同。有关详细信息,请参阅各个属性的文档,以及 Qt Quick 中的动画和转换介绍。

请注意,PropertyAnimation 继承了抽象的Animation 类型。这包括控制动画的附加属性和方法。

修改运行中的动画

自 Qt XML 6.4 起,可以在顶层动画运行时设置from,to,duration, 和easing 属性。动画将在下一次循环中考虑这些更改。

另请参阅 Qt QuickQt Quick 示例 -动画中的动画和过渡

属性文档

properties : string

property : string

target : QtObject

targets : list<QtObject> [read-only]

这些属性作为一个集合,用于确定哪些属性应被动画化。单数和复数形式在功能上是相同的,例如

NumberAnimation { target: theItem; property: "x"; to: 500 }

NumberAnimation { targets: theItem; properties: "x"; to: 500 }

单数形式略有优化,因此如果只有一个目标/属性需要动画,应尽量使用单数形式。

targets 属性允许设置多个目标。例如,该属性可将itemAitemBx 属性动画化:

NumberAnimation { targets: [itemA, itemB]; properties: "x"; to: 500 }

在许多情况下,这些属性不需要明确指定,因为它们可以从动画框架中推断出来:

值源/行为当动画作为值源或在行为中使用时,可以推断出默认目标和要动画化的属性名称。
Rectangle {
    id: theRect
    width: 100; height: 100
    color: Qt.rgba(0,0,1)
    NumberAnimation on x { to: 500; loops: Animation.Infinite } //animate theRect's x property
    Behavior on y { NumberAnimation {} } //animate theRect's y property
}
转换在转换中使用时,属性动画会被假定为匹配所有目标,但匹配任何属性。在实践中,这意味着至少需要指定属性才能使动画生效。
Rectangle {
    id: theRect
    width: 100; height: 100
    color: Qt.rgba(0,0,1)
    Item { id: uselessItem }
    states: State {
        name: "state1"
        PropertyChanges {
            theRect {
                x: 200
                y: 200
                z: 4
            }
            uselessItem {
                x: 10
                y: 10
                z: 2
            }
        }
    }
    transitions: Transition {
        //animate both theRect's and uselessItem's x and y to their final values
        NumberAnimation { properties: "x,y" }

        //animate theRect's z to its final value
        NumberAnimation { target: theRect; property: "z" }
    }
}
独立使用独立使用动画时,需要明确指定目标和属性。
Rectangle {
    id: theRect
    width: 100; height: 100
    color: Qt.rgba(0,0,1)
    //need to explicitly specify target and property
    NumberAnimation { id: theAnim; target: theRect; property: "x"; to: 500 }
    MouseArea {
        anchors.fill: parent
        onClicked: theAnim.start()
    }
}

如上例所示,properties 是以逗号分隔的属性名称字符串指定的动画。

另请参阅 exclude Qt Quick 中的动画和过渡


duration : int

该属性表示动画的持续时间,单位为毫秒。

默认值为 250。


easing group

easing.amplitude : real

easing.bezierCurve : list<real>

easing.overshoot : real

easing.period : real

easing.type : enumeration

指定动画使用的缓和曲线

要指定一条缓和曲线,至少需要指定其类型。对于某些曲线,还可以指定振幅、周期和/或过冲(表后提供了更多详细信息)。默认的缓和曲线是Easing.Linear

PropertyAnimation { properties: "y";
                    easing.type: Easing.InOutElastic;
                    easing.amplitude: 2.0;
                    easing.period: 1.5 }

可用的类型有

Easing.Linear线性 (t) 函数的缓和曲线:速度恒定。
Easing.InQuad二次函数 (t^2) 的缓和曲线:从零开始加速。
Easing.OutQuad二次函数(t^2)的缓和曲线:减速至零速度。
Easing.InOutQuad二次函数 (t^2) 的平缓曲线:加速到一半,然后减速。
Easing.OutInQuad二次函数 (t^2) 的缓和曲线:减速至一半,然后加速。
Easing.InCubic三次(t^3)函数的缓和曲线:从零加速。
Easing.OutCubic三次(t^3)函数的平缓曲线:减速至零速。
Easing.InOutCubic三次(t^3)函数的平缓曲线:加速到一半,然后减速。
Easing.OutInCubic三次(t^3)函数的缓和曲线:减速至一半,然后加速。
Easing.InQuart四次函数 (t^4) 的缓和曲线:从零加速。
Easing.OutQuart四次函数 (t^4) 的缓和曲线:减速至零速。
Easing.InOutQuart四次(t^4)函数的平缓曲线:加速到一半,然后减速。
Easing.OutInQuart四次函数 (t^4) 的缓和曲线:减速至一半,然后加速。
Easing.InQuint五次函数 (t^5) 的缓和曲线:从零加速。
Easing.OutQuint五次函数 (t^5) 的缓和曲线:减速至零速。
Easing.InOutQuint五次函数(t^5)的平缓曲线:加速到一半,然后减速。
Easing.OutInQuint五次函数(t^5)的缓和曲线:减速至一半,然后加速。
Easing.InSine正弦函数(sin(t))的缓和曲线:从零加速。
Easing.OutSine正弦(sin(t))函数的平缓曲线:减速至零速。
Easing.InOutSine正弦(sin(t))函数的缓和曲线:加速到一半,然后减速。
Easing.OutInSine正弦(sin(t))函数的缓和曲线:减速至一半,然后加速。
Easing.InExpo指数函数 (2^t) 的缓和曲线:从零加速。
Easing.OutExpo指数 (2^t) 函数的缓和曲线:减速至零速。
Easing.InOutExpo指数 (2^t) 函数的平缓曲线:加速到一半,然后减速。
Easing.OutInExpo指数 (2^t) 函数的缓和曲线:减速至一半,然后加速。
Easing.InCirc圆 (sqrt(1-t^2)) 函数的缓和曲线:从零加速。
Easing.OutCirc圆 (sqrt(1-t^2)) 函数的平缓曲线:减速至零速度。
Easing.InOutCirc圆周 (sqrt(1-t^2)) 函数的平缓曲线:加速到一半,然后减速。
Easing.OutInCirc圆 (sqrt(1-t^2)) 函数的缓和曲线:减速至一半,然后加速。
Easing.InElastic弹性(指数衰减正弦波)函数的缓和曲线:从零加速。
可以用振幅参数设置峰值振幅,用周期参数设置衰减周期。
Easing.OutElastic弹性(指数衰减正弦波)函数的缓和曲线:减速至零速。
峰值振幅可通过振幅参数设置,衰减周期可通过周期参数设置。
Easing.InOutElastic弹性(指数衰减正弦波)函数的缓和曲线:加速到一半,然后减速。
Easing.OutInElastic弹性(指数衰减正弦波)函数的缓和曲线:减速至一半,然后加速。
Easing.InBack后退(过冲三次函数:(s+1)*t^3 - s*t^2)的缓和曲线:从零加速。
Easing.OutBack后退的缓和曲线(过冲三次函数:(s+1)*t^3 - s*t^2)缓和退出:减速至零速度。
Easing.InOutBack后退的缓和曲线(过冲三次函数:(s+1)*t^3 - s*t^2)缓入/缓出:加速到一半,然后减速。
Easing.OutInBack回弹的缓和曲线(过冲三次函数缓和:(s+1)*t^3 - s*t^2)缓和输出/输入:减速至中途,然后加速。
Easing.InBounce反弹(指数衰减抛物线反弹)函数的缓和曲线:从零开始加速。
Easing.OutBounce弹跳(指数衰减抛物线弹跳)函数的平缓曲线:减速至零速。
Easing.InOutBounce弹跳(指数衰减抛物线弹跳)函数的缓进/缓出曲线:加速到一半,然后减速。
Easing.OutInBounce弹跳(指数衰减抛物线弹跳)函数的缓和曲线 缓出/缓入:减速至一半,然后加速。
Easing.BezierSpline由 easing.bezierCurve 属性定义的自定义缓和曲线。

easing.amplitude 仅适用于反弹和弹性曲线(类型为 , , , , , , 或 的曲线)。Easing.InBounce Easing.OutBounce Easing.InOutBounce Easing.OutInBounce Easing.InElastic Easing.OutElastic Easing.InOutElastic Easing.OutInElastic

easing.overshoot 仅适用于 为, , 或 。easing.type Easing.InBack Easing.OutBack Easing.InOutBack Easing.OutInBack

easing.period 仅当 easing.type 为:, , 或 。Easing.InElastic Easing.OutElastic Easing.InOutElastic Easing.OutInElastic

easing.bezierCurve 仅当 easing.type 为: 时才适用。此属性是一个 list<real>(列表<real>),包含从 0,0 到 1,1 定义曲线的三个点 - control1、control2、end point:[cx1、cy1、cx2、cy2、endx、endy、...]。最后一点必须是 1,1。Easing.BezierSpline

有关不同缓和设置的演示,请参阅缓和曲线


exclude : list<QtObject> [read-only]

此属性包含不受动画影响的项目。

另请参阅 PropertyAnimation::targets


from : variant

该属性用于保存动画的起始值。

如果PropertyAnimation 定义在TransitionBehavior 中,则该值默认为Transition 的起始状态中定义的值,或Behavior 被触发时该属性的当前值。

另请参阅 Qt Quick 中的动画和过渡


to : variant

该属性保存动画的结束值。

如果PropertyAnimation 定义在TransitionBehavior 中,则该值默认为Transition 的结束状态中定义的值,或触发Behavior 的属性变化值。

另请参阅 Qt Quick 中的动画和过渡


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