[844] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
| 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Free Documentation License
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
| 18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
| 20 | ** file.
|
---|
| 21 | **
|
---|
| 22 | ** If you have questions regarding the use of this file, please contact
|
---|
| 23 | ** Nokia at [email protected].
|
---|
| 24 | ** $QT_END_LICENSE$
|
---|
| 25 | **
|
---|
| 26 | ****************************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
| 29 | \page qdeclarativeanimation.html
|
---|
| 30 | \title QML Animation
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | In QML, animations are created by applying animation objects to object property
|
---|
| 34 | values to gradually change them over time. Animation objects are created from
|
---|
| 35 | the built-in set of animation elements, which can be used to animate various
|
---|
| 36 | types of property values. In addition, animation objects can be applied in
|
---|
| 37 | different ways depending on the context in which they are required.
|
---|
| 38 |
|
---|
| 39 | To create an animation, use an appropriate animation element for the type of
|
---|
| 40 | the property that is to be animated, and apply the animation depending on the
|
---|
| 41 | type of behavior that is required. This page describes the \l {Types of
|
---|
| 42 | Animations} that can be created and the \l {Animation Elements} that are used
|
---|
| 43 | to create these animations.
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | \section1 Types of Animations
|
---|
| 47 |
|
---|
| 48 | An animation is created in different ways depending on the context in which it
|
---|
| 49 | is required. Suppose a \l Rectangle's movement - that is, changes in its \c x
|
---|
| 50 | or \c y property values - should be animated. The semantics of the animation
|
---|
| 51 | differ depending on whether you want to create:
|
---|
| 52 |
|
---|
| 53 | \list
|
---|
| 54 | \o An animation that moves the \l Rectangle as soon as it is created, to a
|
---|
| 55 | known position
|
---|
| 56 | \o An animation that only triggers when the \l Rectangle is moved by external
|
---|
| 57 | sources - for example, when the mouse is clicked, animate the movement to the
|
---|
| 58 | mouse position
|
---|
| 59 | \o An animation that triggers when a particular signal is received
|
---|
| 60 | \o A standalone animation that is not bound to the \l Rectangle's movement, but
|
---|
| 61 | instead can be started and stopped from script as required
|
---|
| 62 | \o An animation that only triggers during \l{QML States}{state changes}
|
---|
| 63 | \endlist
|
---|
| 64 |
|
---|
| 65 | To support these different types of animation methods, QML provides several
|
---|
| 66 | methods for defining an animation. These are:
|
---|
| 67 |
|
---|
| 68 | \list
|
---|
| 69 | \o Creating an \l{Animations as Property Value Sources}{animation using
|
---|
| 70 | property value sources}, to immediately animate a specific property
|
---|
| 71 | \o Using \l{Behavioral Animations}{behavioral animations}, which are triggered
|
---|
| 72 | when a property changes value
|
---|
| 73 | \o \l{Animations in a Signal Handler}{Within a signal handler}, to be triggered
|
---|
| 74 | when a signal is received
|
---|
| 75 | \o As a \l{Standalone Animation}{standalone animation}, that can be
|
---|
| 76 | started/stopped from script and can be rebound to different objects
|
---|
| 77 | \o Using \l{Transitions}{transitions}, to provide animations between \l{QML
|
---|
| 78 | States}{state changes}
|
---|
| 79 | \endlist
|
---|
| 80 |
|
---|
| 81 | These methods are demonstrated below. Notice these examples use
|
---|
| 82 | PropertyAnimation, which is one of several QML elements that can be used to
|
---|
| 83 | create an animation. See the \l {Animation Elements} section further below for
|
---|
| 84 | details.
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | \section2 Animations as Property Value Sources
|
---|
| 89 |
|
---|
| 90 | An animation is applied as a \l{QDeclarativePropertyValueSource}{property value
|
---|
| 91 | source} using the \e Animation \bold on \e Property syntax. Here is a \l
|
---|
| 92 | Rectangle whose movement is animated using this method:
|
---|
| 93 |
|
---|
| 94 | \snippet doc/src/snippets/declarative/animation-propertyvaluesource.qml 0
|
---|
| 95 |
|
---|
| 96 | This applies a PropertyAnimation to the \l Rectangle's \c x and \c y properties
|
---|
| 97 | to animate from their current values (i.e. zero) to 50, over 1000 milliseconds.
|
---|
| 98 | The animation starts as soon as the \l Rectangle is loaded. To animate from
|
---|
| 99 | specific values rather than the current \c x and \c y values, set the
|
---|
| 100 | PropertyAnimation's \l {PropertyAnimation::}{from} property.
|
---|
| 101 |
|
---|
| 102 | Specifying an animation as a property value source is useful for animating a
|
---|
| 103 | property to a particular value as soon as the object is loaded.
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | \section2 Behavioral Animations
|
---|
| 107 |
|
---|
| 108 | Often an animation should be applied whenever a particular property value
|
---|
| 109 | changes. In these cases, a \l Behavior can be used to specify a default
|
---|
| 110 | animation for a property change. Here is an example:
|
---|
| 111 |
|
---|
| 112 | \snippet doc/src/snippets/declarative/animation-behavioral.qml 0
|
---|
| 113 |
|
---|
| 114 | This \l Rectangle has \l Behavior objects applied to its \c x and \c y
|
---|
| 115 | properties. Whenever these properties change (in this case, when the mouse is
|
---|
| 116 | clicked within the parent \l Item), the PropertyAnimation objects defined
|
---|
| 117 | within the behaviors will be applied to these properties, thus animating the \l
|
---|
| 118 | Rectangle's movement to its new position. Unlike the method of \l {Animations
|
---|
| 119 | as Property Value Sources}{defining an animation as a property value source},
|
---|
| 120 | which creates a one-time animation that animates a property to a known value, a
|
---|
| 121 | behavioral animation is an animation that is triggered \e {in response to} a
|
---|
| 122 | value change.
|
---|
| 123 |
|
---|
| 124 | Any changes to these properties will trigger their animations. If \c x or \c y
|
---|
| 125 | were bound to other properties, and those properties changed, the animation
|
---|
| 126 | would be triggered. The \l{Behavior::}{enabled} property can be used to force a
|
---|
| 127 | \l Behavior to only apply under certain circumstances.
|
---|
| 128 |
|
---|
| 129 | Notice that unlike for property value source animations, the
|
---|
| 130 | PropertyAnimation's \l {PropertyAnimation::}{from} and \l
|
---|
| 131 | {PropertyAnimation::}{to} properties do not need to be defined because these
|
---|
| 132 | values are already provided, respectively, by the \l Rectangle's current values
|
---|
| 133 | and the new values set in the \c onClicked handler. If these properties were
|
---|
| 134 | defined anyway, they would override the default values.
|
---|
| 135 |
|
---|
| 136 | See the \l {declarative/animation/behaviors}{Behaviors example} for a
|
---|
| 137 | demonstration of behavioral animations.
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | \section2 Animations in a Signal Handler
|
---|
| 141 |
|
---|
| 142 | An animation can be created within a signal handler to be triggered when the
|
---|
| 143 | signal is received. For example:
|
---|
| 144 |
|
---|
| 145 | \snippet doc/src/snippets/declarative/animation-signalhandler.qml 0
|
---|
| 146 |
|
---|
| 147 | The PropertyAnimation is triggered when the MouseArea is clicked, animating the
|
---|
| 148 | \c x and \c y properties to a value of 50 over 1000 milliseconds. Since the
|
---|
| 149 | animation is not bound to a particular object or property, it must define the
|
---|
| 150 | \l {PropertyAnimation::}{target} and \l {PropertyAnimation::}{property} (or \l
|
---|
| 151 | {PropertyAnimation::}{targets} and \l{PropertyAnimation::}{properties}) values.
|
---|
| 152 | The \l {PropertyAnimation::}{to} property is also required to specify the new
|
---|
| 153 | \c x and \c y values.
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | \section2 Standalone Animations
|
---|
| 157 |
|
---|
| 158 | Animations can also be created as ordinary QML objects that are not bound to
|
---|
| 159 | any particular objects and properties. Here is an example, using a
|
---|
| 160 | PropertyAnimation object. The animation is explicitly started when the
|
---|
| 161 | \l Rectangle is clicked:
|
---|
| 162 |
|
---|
| 163 | \snippet doc/src/snippets/declarative/animation-standalone.qml 0
|
---|
| 164 |
|
---|
| 165 | A standalone animation object is not running by default and must be started explicitly
|
---|
| 166 | using the \l {Animation::}{running} property or \l {Animation::}{start()} and
|
---|
| 167 | \l {Animation::}{stop()} methods. Since the animation is not bound to a
|
---|
| 168 | particular object or property, it must define the \l
|
---|
| 169 | {PropertyAnimation::}{target} and \l {PropertyAnimation::}{property} (or \l
|
---|
| 170 | {PropertyAnimation::}{targets} and \l{PropertyAnimation::}{properties}) values.
|
---|
| 171 | The \l {PropertyAnimation::}{to} property is also required to specify the new
|
---|
| 172 | \c x and \c y values. (The \l {PropertyAnimation::}{from} value can optionally
|
---|
| 173 | be provided.)
|
---|
| 174 |
|
---|
| 175 | Standalone animations are useful when an animation is not targeted towards a
|
---|
| 176 | single object property and the animation should be explicitly started and
|
---|
| 177 | stopped.
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | \section2 Transitions
|
---|
| 181 |
|
---|
| 182 | Transitions are used to describe the animations to be applied when a \l {QML
|
---|
| 183 | States}{state change} occurs. To create a transition, define a \l Transition
|
---|
| 184 | object and add it to an item's \l {Item::}{transitions} property. An example:
|
---|
| 185 |
|
---|
| 186 | \snippet doc/src/snippets/declarative/animation-transitions.qml 0
|
---|
| 187 |
|
---|
| 188 | The PropertyChanges object in the \e moved state defines that when the
|
---|
| 189 | \l Rectangle is in this state, its position should be changed
|
---|
| 190 | to (50, 50). When the \l Rectangle changes to the \e moved state, the
|
---|
| 191 | \l Transition will be triggered, and the transition's \l PropertyAnimation will
|
---|
| 192 | animate the changes in the \c x and \c y properties to their new values.
|
---|
| 193 | The animation will not be applied at any time other than during the state
|
---|
| 194 | change.
|
---|
| 195 |
|
---|
| 196 | Notice the example does not set any \l {PropertyAnimation::}{from} and \l
|
---|
| 197 | {PropertyAnimation::}{to} values for the PropertyAnimation. As a convenience,
|
---|
| 198 | these properties are automatically set to the values of \c x and \c y before
|
---|
| 199 | and after the state change, respectively. However, they can be explicitly set
|
---|
| 200 | if these values should be overrided.
|
---|
| 201 |
|
---|
| 202 | Also notice the PropertyAnimation does not need to specify a \l
|
---|
| 203 | {PropertyAnimation::}{target} object; any \c x or \c y value of any object that
|
---|
| 204 | has changed during the state change will be animated. However, the target can
|
---|
| 205 | be set if the animation should be restricted to certain objects.
|
---|
| 206 |
|
---|
| 207 | The top-level animations in a \l Transition are run in parallel. To run them
|
---|
| 208 | one after the other, use a SequentialAnimation, as shown below in \l {Grouping
|
---|
| 209 | Animations}.
|
---|
| 210 |
|
---|
| 211 | See the \l Transition documentation for more information.
|
---|
| 212 |
|
---|
| 213 |
|
---|
| 214 | \section1 Animation Elements
|
---|
| 215 |
|
---|
| 216 | To create an animation, choose from one of the built-in QML animation elements.
|
---|
| 217 | While the above examples are demonstrated using PropertyAnimation, they could
|
---|
| 218 | have used other elements depending on the type of the property to be animated
|
---|
| 219 | and whether a single or multiple animations are required.
|
---|
| 220 |
|
---|
| 221 | All animation elements inherit from the \l Animation element. It is not
|
---|
| 222 | possible to create \l Animation objects; instead, this element provides the
|
---|
| 223 | essential properties and methods for animation elements. For example, it allows
|
---|
| 224 | animations to be started and stopped through the \l {Animation::}{running}
|
---|
| 225 | property and the \l{Animation::}{start()} and \l{Animation::}{stop()} methods.
|
---|
| 226 | It can also define the number of \l {Animation::}{loops} for an animation.
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | \section2 Property Animation Elements
|
---|
| 230 |
|
---|
| 231 | PropertyAnimation is the most basic animation element for animating a property.
|
---|
| 232 | It can be used to animate \c real, \c int, \c color, \c rect, \c point, \c size, and
|
---|
| 233 | \c vector3d properties. It is inherited by NumberAnimation, ColorAnimation,
|
---|
| 234 | RotationAnimation and Vector3dAnimation: NumberAnimation provides a more
|
---|
| 235 | efficient implementation for animating \c real and \c int properties, and
|
---|
| 236 | Vector3dAnimation does the same for \c vector3d properties. ColorAnimation
|
---|
| 237 | and RotationAnimation provide more specific attributes for animating color
|
---|
| 238 | and rotation changes.
|
---|
| 239 |
|
---|
| 240 | A ColorAnimation allows color values for the \l {ColorAnimation::}{from}
|
---|
| 241 | and \l {ColorAnimation::}{to} properties. The
|
---|
| 242 | following animates the rectangle's \l {Rectangle::}{color} property:
|
---|
| 243 |
|
---|
| 244 | \snippet doc/src/snippets/declarative/animation-elements.qml color
|
---|
| 245 |
|
---|
| 246 | RotationAnimation allows a rotation's direction to be specified. The following
|
---|
| 247 | animates the rectangle's \l {Item::rotation} property:
|
---|
| 248 |
|
---|
| 249 | \snippet doc/src/snippets/declarative/animation-elements.qml rotation
|
---|
| 250 |
|
---|
| 251 | In addition, the following specialized animation elements are available:
|
---|
| 252 |
|
---|
| 253 | \list
|
---|
| 254 | \o SmoothedAnimation: a specialized NumberAnimation that provides smooth
|
---|
| 255 | changes in animation when the target value changes
|
---|
| 256 | \o SpringAnimation: provides a spring-like animation with specialized
|
---|
| 257 | attributes such as \l {SpringAnimation::}{mass},
|
---|
| 258 | \l{SpringAnimation::}{damping} and \l{SpringAnimation::}{epsilon}
|
---|
| 259 | \o ParentAnimation: used for animating a parent change (see ParentChange)
|
---|
| 260 | \o AnchorAnimation: used for animating an anchor change (see AnchorChanges)
|
---|
| 261 | \endlist
|
---|
| 262 |
|
---|
| 263 | See their respective documentation pages for more details.
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | \section3 Easing
|
---|
| 267 |
|
---|
| 268 | Any PropertyAnimation-based animations can specify \l
|
---|
| 269 | {PropertyAnimation::easing.type}{easing attributes} to control the
|
---|
| 270 | easing curve applied when a property value is animated. These control the
|
---|
| 271 | effect of the animation on the property value, to provide visual effects like
|
---|
| 272 | bounce, acceleration and deceleration.
|
---|
| 273 |
|
---|
| 274 | For example, this modified version of an \l {Animations as Property Value
|
---|
| 275 | Sources}{earlier example} uses \c Easing.OutBounce to create a bouncing effect
|
---|
| 276 | when the animation reaches its target value:
|
---|
| 277 |
|
---|
| 278 | \snippet doc/src/snippets/declarative/animation-easing.qml 0
|
---|
| 279 |
|
---|
| 280 | The \l{declarative/animation/easing}{easing example} visually demonstrates each
|
---|
| 281 | of the different easing types.
|
---|
| 282 |
|
---|
| 283 | \section2 Grouping Animations
|
---|
| 284 |
|
---|
| 285 | Multiple animations can be combined into a single animation using one of the
|
---|
| 286 | animation group elements: ParallelAnimation or SequentialAnimation. As their
|
---|
| 287 | names suggest, animations in a ParallelAnimation are run at the same time,
|
---|
| 288 | while animations in a SequentialAnimation are run one after the other.
|
---|
| 289 |
|
---|
| 290 | To run multiple animations, define the animations within an animation group.
|
---|
| 291 | The following example creates a SequentialAnimation that runs three animations
|
---|
| 292 | one after the other: a NumberAnimation, a PauseAnimation and another
|
---|
| 293 | NumberAnimation. The SequentialAnimation is applied as a \l{Animations as
|
---|
| 294 | Property Value Sources}{property value source animation} on the image's \c y
|
---|
| 295 | property, so that the animation starts as soon as the image is loaded, moving
|
---|
| 296 | the image up and down:
|
---|
| 297 |
|
---|
| 298 | \snippet doc/src/snippets/declarative/animation-groups.qml 0
|
---|
| 299 | \image propanim.gif
|
---|
| 300 |
|
---|
| 301 | Since the SequentialAnimation is applied to the \c y property, the individual
|
---|
| 302 | animations within the group are automatically applied to the \c y property as
|
---|
| 303 | well; it is not required to set their \l{PropertyAnimation::}{properties}
|
---|
| 304 | values to a particular property.
|
---|
| 305 |
|
---|
| 306 | Animation groups can be nested. Here is a rather complex animation making use
|
---|
| 307 | of both sequential and parallel animations:
|
---|
| 308 |
|
---|
| 309 | \snippet doc/src/snippets/declarative/animation-groups.qml 1
|
---|
| 310 |
|
---|
| 311 | Once individual animations are placed into a SequentialAnimation or
|
---|
| 312 | ParallelAnimation, they can no longer be started and stopped independently. The
|
---|
| 313 | sequential or parallel animation must be started and stopped as a group.
|
---|
| 314 |
|
---|
| 315 | See the \l {declarative/animation/basics}{Animation basics example} for a
|
---|
| 316 | demonstration of creating and combining multiple animations in QML.
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 |
|
---|
| 320 | \section2 Other Animation Elements
|
---|
| 321 |
|
---|
| 322 | In addition, QML provides several other elements useful for animation:
|
---|
| 323 |
|
---|
| 324 | \list
|
---|
| 325 | \o PauseAnimation: enables pauses during animations
|
---|
| 326 | \o ScriptAction: allows JavaScript to be executed during an animation, and can
|
---|
| 327 | be used together with StateChangeScript to reused existing scripts
|
---|
| 328 | \o PropertyAction: changes a property \e immediately during an animation,
|
---|
| 329 | without animating the property change
|
---|
| 330 | \endlist
|
---|
| 331 |
|
---|
| 332 | See their respective documentation pages for more details.
|
---|
| 333 |
|
---|
| 334 | */
|
---|