C

Animation and transitions

Animation and transition types

Types that animate properties based on data types:

ColorAnimation

Animates changes in color values

NumberAnimation

Animates changes in qreal-type values

PropertyAnimation

Animates changes in property values

RotationAnimation

Animates changes in rotation values

Animations are created by applying animation types to property values. Animation types will interpolate property values to create smooth transitions. Also, state transitions may assign animations to state changes.

To create an animation, use an appropriate animation type for the type of the property that is to be animated, and apply the animation depending on the type of behavior that is required.

Triggering animations

The sections below describe different ways of setting animation to an object.

Direct property animation

Animations are created by applying animation objects to property values to gradually change the properties over time. These property animations apply smooth movements by interpolating values between property value changes. Property animations provide timing controls and allow different interpolations through easing curves.

Rectangle {
    id: flashingblob
    width: 75; height: 75
    color: "blue"
    opacity: 1.0

    MouseArea {
        anchors.fill: parent
        onClicked: {
            animateColor.start()
            animateOpacity.start()
        }
    }

    PropertyAnimation {id: animateColor; target: flashingblob; properties: "color"; to: "green"; duration: 100}

    NumberAnimation {
        id: animateOpacity
        target: flashingblob
        properties: "opacity"
        from: 0.5
        to: 1.0
        loops: Animation.Infinite
        easing {type: Easing.OutQuad}
   }
}

Specialized property animation types have more efficient implementations than the PropertyAnimation type. They are for setting animations to different QML types such as int, color, and rotations.

For more information about the different animation properties, see Controlling animations.

Using predefined targets and properties

In the previous example, the PropertyAnimation and NumberAnimation objects needed to specify particular target and properties values to specify the objects and properties that should be animated. This can be avoided by using the <Animation> on <Property> syntax, which specifies the animation is to be applied as a property value source.

Below are two PropertyAnimation objects that are specified using this syntax:

import QtQuick 2.15

Rectangle {
    id: rect
    width: 100; height: 100
    color: "red"

    PropertyAnimation on x { to: 100 }
    PropertyAnimation on y { to: 100 }
}

The animation starts as soon as the rectangle is loaded, and is applied to its x and y values automatically. Since the <Animation> on <Property> syntax has been used, it is not necessary to set the target value of the PropertyAnimation objects to rect, and neither is it necessary to set the property values to x and y.

This can also be used by grouped animations to ensure that all animations within a group are applied to the same property. For example, the previous example could instead use SequentialAnimation to animate the rectangle's color first to yellow, then to blue:

import QtQuick 2.15

Rectangle {
    width: 100; height: 100
    color: "red"

    SequentialAnimation on color {
        ColorAnimation { to: "yellow"; duration: 1000 }
        ColorAnimation { to: "blue"; duration: 1000 }
    }
}

Since the SequentialAnimation object has been specified on the color property using the <Animation> on <Property> syntax, its child ColorAnimation objects are also automatically applied to this property and do not need to specify target or property animation values.

Transitions during state changes

States are property configurations where a property may have different values to reflect different states. State changes introduce abrupt property changes, whereas animations introduce smooth transitions to produce visually appealing state changes.

The Transition type can contain animation types to interpolate property changes caused by state changes. To assign the transition to an object, bind it to the transitions property.

A button might have two states, the pressed state when the user selects the button, and a released state when the user releases the button. You can assign different property configurations for each state. A transition would animate the change from the selected state to the released state. Likewise, there would be an animation during the change from the released state to the selected state.

Rectangle {
    width: 75; height: 75
    id: button
    state: "RELEASED"

    MouseArea {
        anchors.fill: parent
        onPressed: button.state = "PRESSED"
        onReleased: button.state = "RELEASED"
    }

    states: [
        State {
            name: "PRESSED"
            PropertyChanges { target: button; color: "lightblue"}
        },
        State {
            name: "RELEASED"
            PropertyChanges { target: button; color: "lightsteelblue"}
        }
    ]

    transitions: [
        Transition {
            from: "PRESSED"
            to: "RELEASED"
            ColorAnimation { target: button; duration: 100}
        },
        Transition {
            from: "RELEASED"
            to: "PRESSED"
            ColorAnimation { target: button; duration: 100}
        }
    ]
}

Binding the to and from properties to the state's name will assign that particular transition to the state change. For simple or symmetric transitions, setting the to to property to the wild card symbol, "*", denotes that the transition applies to any state change.

    transitions:
        Transition {
            to: "*"
            ColorAnimation { target: button; duration: 100}
        }

Default animation as behaviors

Default property animations are set using behavior animations. Animations declared in Behavior types apply to the property and animates any property value changes. However, Behavior types have an enabled property to purposely enable or disable the behavior animations.

A ball component might have a behavior animation assigned to its x, y, and color properties. The behavior animation could be set up to simulate an elastic effect. In effect, this behavior animation would apply the elastic effect to the properties whenever the ball moves.

Rectangle {
    width: 75; height: 75; radius: width
    id: ball
    color: "salmon"

    Behavior on x {
        NumberAnimation {
            id: bouncebehavior
            easing {
                type: Easing.OutSine
            }
        }
    }
    Behavior on y {
        animation: bouncebehavior
    }
    Behavior {
        ColorAnimation { target: ball; duration: 100 }
    }
}

There are several methods of assigning behavior animations to properties. The Behavior on <property> declaration is a convenient way of assigning a behavior animation onto a property.

Playing animations in parallel or in sequence

Animations can run in parallel or in sequence. Parallel animations will play a group of animations at the same time, while sequential animations play a group of animations one after another. Grouping animations in SequentialAnimation and ParallelAnimation will play the animations in sequence or in parallel.

A banner component may have several icons or slogans to display, one after another. The opacity property could transform to 1.0 denoting an opaque object. Using the SequentialAnimation type, the opacity animations will play after the preceding animation finishes. The ParallelAnimation type will play the animations at the same time.

Rectangle {
    id: banner

    Column {
        anchors.centerIn: parent
        Text {
            id: code
            text: "Code less."
            opacity: 0.01
        }
        Text {
            id: create
            text: "Create more."
            opacity: 0.01
        }
        Text {
            id: deploy
            text: "Deploy everywhere."
            opacity: 0.01
        }
    }

    MouseArea {
        anchors.fill: parent
        onPressed: playbanner.start()
    }

    SequentialAnimation {
        id: playbanner
        running: false
        NumberAnimation { target: code; property: "opacity"; to: 1.0; duration: 200}
        NumberAnimation { target: create; property: "opacity"; to: 1.0; duration: 200}
        NumberAnimation { target: deploy; property: "opacity"; to: 1.0; duration: 200}
    }
}

Once individual animations are placed into a SequentialAnimation or ParallelAnimation, they can no longer be started and stopped independently. The sequential or parallel animation must be started and stopped as a group.

The SequentialAnimation type is also useful for playing transition animations because animations are played in parallel inside transitions.

Controlling animations

The sections below describe different methods to control animations.

Animation playback

All animation types inherit from the Animation type. It is not possible to create Animation objects; instead, this type provides the essential properties and methods for animation types. Animation types have start() and stop(), which control the execution of animations.

Easing

Easing curves define how the animation will interpolate between the start value and the end value. Different easing curves might go beyond the defined range of interpolation. The easing curves simplify the creation of animation effects such as bounce effects, acceleration, deceleration, and cyclical animations.

A QML object may have a different easing curve for each property animation. There are also different parameters to control the curve, some of which are exclusive to a particular curve. For more information about the easing curves, visit the easing documentation.

Other animation types

In addition, QML provides several other types useful for animation:

  • PauseAnimation: Enables pauses during animations.
  • ScriptAction: Allows JavaScript to be executed during an animation, and can be used together with StateChangeScript to reused existing scripts.

Sharing animation instances

Sharing animation instances between Transitions or Behaviors is not supported, and may lead to undefined behavior. In the following example, changes to the Rectangle's position will most likely not be correctly animated.

Rectangle {
    // NOT SUPPORTED: this will not work correctly as both Behaviors
    // try to control a single animation instance.
    NumberAnimation { id: anim; duration: 300; easing.type: Easing.InBack }
    Behavior on x { animation: anim }
    Behavior on y { animation: anim }
}

The easiest fix is to repeat the NumberAnimation for both Behaviors. If the repeated animation is complex, you might also consider creating a custom animation component and assigning an instance to each Behavior. For example:

// MyNumberAnimation.qml
NumberAnimation { id: anim; duration: 300; easing.type: Easing.InBack }
// main.qml
Rectangle {
    Behavior on x { MyNumberAnimation {} }
    Behavior on y { MyNumberAnimation {} }
}

Available under certain Qt licenses.
Find out more.