本页内容

C

PropertyAnimation QML Type

为属性值的更改添加动画效果。更多...

Import Statement: import QtQuick
Since: Qt Quick Ultralite 1.0
Inherits:

Animation

Inherited By:

ColorAnimation, CoordinateAnimation, NumberAnimation, RotationAnimation, and TimelineAnimation

属性

详细说明

PropertyAnimation 提供了一种对属性值的变化进行动画处理的方法。

它可以通过多种方式定义动画:

  • 在Transition

    例如,要对因状态变化而修改了x 或y 属性的对象应用动画效果,并使用InOutQuad 缓动曲线:

    Item {
        id: root
    
        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 }
        }
    }
  • 独立模式

    例如,要将rect 的width 属性在500毫秒内从当前宽度动画过渡到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 }
    }

根据动画的使用方式不同,通常会用到的属性集也会有所不同。有关更多信息,请参阅各属性的文档,以及《动画与过渡》简介。

请注意,PropertyAnimation 继承了抽象类型Animation 。该类型包含用于控制动画的额外属性和方法。

另请参阅 “动画与过渡”。

属性文档

duration : int

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

默认值为 250。

easing group

easing.bezierCurve : list<real>

easing.type : enumeration

指定动画所使用的缓动曲线

要指定缓动曲线,至少需要指定其类型。默认的缓动曲线为Easing.Linear 。

PropertyAnimation { properties: "y";
                    easing.type: Easing.InOutSine;
                  }

可用类型包括:

Easing.Linear线性(t)函数的缓动曲线:速度保持恒定。
Easing.InQuad二次(t²)函数的缓动曲线:从零速度开始加速。
Easing.OutQuad二次(t²)函数的缓动曲线:从零速度减速至零速度。
Easing.InOutQuad二次(t²)函数的缓动曲线:加速至中途,然后减速。
Easing.OutInQuad二次(t²)函数的缓入曲线:减速至行程一半,然后加速。
Easing.InCubic三次(t³)函数的缓动曲线:从零速度开始加速。
Easing.OutCubic三次(t^3)函数的缓动曲线:减速至零速度。
Easing.InOutCubic三次(t^3)函数的缓动曲线:加速至行程一半,然后减速。
Easing.OutInCubic三次(t³)函数的缓入曲线:减速至中途,然后加速。
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弹性(指数衰减正弦波)函数的缓动曲线:从零速度开始加速。它使用峰值振幅(1.0)和衰减周期(0.3)的默认值。
Easing.OutElastic弹性(指数衰减正弦波)函数的缓入缓出曲线:减速至零速度。它使用峰值振幅(1.0)和衰减周期(0.3)的默认值。
Easing.InOutElastic弹性(指数衰减正弦波)函数的缓动曲线:加速至中途,随后减速。
Easing.OutInElastic弹性(指数衰减正弦波)函数的缓入曲线:减速至半程,然后加速。
Easing.InBack回弹(过冲三次函数:(s+1)*t^3 - s*t^2)缓入的缓动曲线:从零速度开始加速。它使用了过冲量(s:1.70158)的默认值。
Easing.OutBack回弹(过冲三次函数:(s+1)*t^3 - s*t^2)淡出缓动曲线:减速至零速度。它使用过冲的默认值(s:1.70158)。
Easing.InOutBack回弹的缓动曲线(超调三次函数:(s+1)*t^3 - s*t^2)入/出缓动:加速至中途,然后减速。它使用超调的默认值(s:1.70158)。
Easing.OutInBack回弹(过冲三次方缓入/缓出:(s+1)*t^3 - s*t^2)缓入/缓出:减速至中途,然后加速。它使用过冲的默认值(s:1.70158)。
Easing.InBounce弹跳(指数衰减抛物线弹跳)函数的缓动曲线:从零速度开始加速。
Easing.OutBounce反弹(指数衰减抛物线反弹)函数的缓动曲线:从零速度开始减速。
Easing.InOutBounce弹跳(指数衰减抛物线弹跳)函数的缓入/缓出缓动曲线:加速至中途,然后减速。
Easing.OutInBounce反弹(指数衰减抛物线反弹)函数的缓入/缓出缓动曲线:减速至半程,然后加速。
Easing.BezierSpline由 easing.bezierCurve 属性定义的自定义缓动曲线。

easing.bezierCurve 仅当 easing.type 为:Easing.BezierSpline 时才适用。该属性是一个 list<real>,包含三点一组,定义了从 0,0 到 1,1 的曲线——控制点 1、控制点 2 和终点:[cx1, cy1, cx2, cy2, endx, endy, ...]。 最后一点必须为 1,1。

注意:若设置了 easing.bezierCurve 但未指定 easing.type,qmltocpp 工具将生成默认将 easing.type 隐式设置为Easing.BezierSpline 的代码。

from : variant

该属性存储动画的起始值。

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

另请参阅 “动画与过渡”。

properties : string

property : string

target : Object

这些属性作为一个集合,用于确定哪些属性应进行动画处理。单数形式和复数形式在功能上完全相同,例如

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

与

NumberAnimation {
    targets theItem;
    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
            }
        }
        PropertyChanges {
            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 被指定为一个以逗号分隔的字符串,其中包含要进行动画处理的属性名称。

另请参阅 “动画”和“过渡”。

to : variant

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

如果PropertyAnimation 在Transition 或Behavior 中被定义,则该值默认为Transition 的end状态中定义的值,或者触发Behavior 的property change的值。

另请参阅 “动画与过渡”。

在某些 Qt 许可证下可用。
了解更多信息。