source: trunk/doc/src/declarative/animation.qdoc@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 14.8 KB
Line 
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
33In QML, animations are created by applying animation objects to object property
34values to gradually change them over time. Animation objects are created from
35the built-in set of animation elements, which can be used to animate various
36types of property values. In addition, animation objects can be applied in
37different ways depending on the context in which they are required.
38
39To create an animation, use an appropriate animation element for the type of
40the property that is to be animated, and apply the animation depending on the
41type of behavior that is required. This page describes the \l {Types of
42Animations} that can be created and the \l {Animation Elements} that are used
43to create these animations.
44
45
46\section1 Types of Animations
47
48An animation is created in different ways depending on the context in which it
49is required. Suppose a \l Rectangle's movement - that is, changes in its \c x
50or \c y property values - should be animated. The semantics of the animation
51differ 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
55known position
56\o An animation that only triggers when the \l Rectangle is moved by external
57sources - for example, when the mouse is clicked, animate the movement to the
58mouse 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
61instead 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
65To support these different types of animation methods, QML provides several
66methods for defining an animation. These are:
67
68\list
69\o Creating an \l{Animations as Property Value Sources}{animation using
70property value sources}, to immediately animate a specific property
71\o Using \l{Behavioral Animations}{behavioral animations}, which are triggered
72when a property changes value
73\o \l{Animations in a Signal Handler}{Within a signal handler}, to be triggered
74when a signal is received
75\o As a \l{Standalone Animation}{standalone animation}, that can be
76started/stopped from script and can be rebound to different objects
77\o Using \l{Transitions}{transitions}, to provide animations between \l{QML
78States}{state changes}
79\endlist
80
81These methods are demonstrated below. Notice these examples use
82PropertyAnimation, which is one of several QML elements that can be used to
83create an animation. See the \l {Animation Elements} section further below for
84details.
85
86
87
88\section2 Animations as Property Value Sources
89
90An animation is applied as a \l{QDeclarativePropertyValueSource}{property value
91source} using the \e Animation \bold on \e Property syntax. Here is a \l
92Rectangle whose movement is animated using this method:
93
94\snippet doc/src/snippets/declarative/animation-propertyvaluesource.qml 0
95
96This applies a PropertyAnimation to the \l Rectangle's \c x and \c y properties
97to animate from their current values (i.e. zero) to 50, over 1000 milliseconds.
98The animation starts as soon as the \l Rectangle is loaded. To animate from
99specific values rather than the current \c x and \c y values, set the
100PropertyAnimation's \l {PropertyAnimation::}{from} property.
101
102Specifying an animation as a property value source is useful for animating a
103property to a particular value as soon as the object is loaded.
104
105
106\section2 Behavioral Animations
107
108Often an animation should be applied whenever a particular property value
109changes. In these cases, a \l Behavior can be used to specify a default
110animation for a property change. Here is an example:
111
112\snippet doc/src/snippets/declarative/animation-behavioral.qml 0
113
114This \l Rectangle has \l Behavior objects applied to its \c x and \c y
115properties. Whenever these properties change (in this case, when the mouse is
116clicked within the parent \l Item), the PropertyAnimation objects defined
117within the behaviors will be applied to these properties, thus animating the \l
118Rectangle's movement to its new position. Unlike the method of \l {Animations
119as Property Value Sources}{defining an animation as a property value source},
120which creates a one-time animation that animates a property to a known value, a
121behavioral animation is an animation that is triggered \e {in response to} a
122value change.
123
124Any changes to these properties will trigger their animations. If \c x or \c y
125were bound to other properties, and those properties changed, the animation
126would be triggered. The \l{Behavior::}{enabled} property can be used to force a
127\l Behavior to only apply under certain circumstances.
128
129Notice that unlike for property value source animations, the
130PropertyAnimation's \l {PropertyAnimation::}{from} and \l
131{PropertyAnimation::}{to} properties do not need to be defined because these
132values are already provided, respectively, by the \l Rectangle's current values
133and the new values set in the \c onClicked handler. If these properties were
134defined anyway, they would override the default values.
135
136See the \l {declarative/animation/behaviors}{Behaviors example} for a
137demonstration of behavioral animations.
138
139
140\section2 Animations in a Signal Handler
141
142An animation can be created within a signal handler to be triggered when the
143signal is received. For example:
144
145\snippet doc/src/snippets/declarative/animation-signalhandler.qml 0
146
147The 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
149animation 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.
152The \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
158Animations can also be created as ordinary QML objects that are not bound to
159any particular objects and properties. Here is an example, using a
160PropertyAnimation 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
165A standalone animation object is not running by default and must be started explicitly
166using the \l {Animation::}{running} property or \l {Animation::}{start()} and
167\l {Animation::}{stop()} methods. Since the animation is not bound to a
168particular 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.
171The \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
173be provided.)
174
175Standalone animations are useful when an animation is not targeted towards a
176single object property and the animation should be explicitly started and
177stopped.
178
179
180\section2 Transitions
181
182Transitions are used to describe the animations to be applied when a \l {QML
183States}{state change} occurs. To create a transition, define a \l Transition
184object 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
188The PropertyChanges object in the \e moved state defines that when the
189\l Rectangle is in this state, its position should be changed
190to (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
192animate the changes in the \c x and \c y properties to their new values.
193The animation will not be applied at any time other than during the state
194change.
195
196Notice the example does not set any \l {PropertyAnimation::}{from} and \l
197{PropertyAnimation::}{to} values for the PropertyAnimation. As a convenience,
198these properties are automatically set to the values of \c x and \c y before
199and after the state change, respectively. However, they can be explicitly set
200if these values should be overrided.
201
202Also 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
204has changed during the state change will be animated. However, the target can
205be set if the animation should be restricted to certain objects.
206
207The top-level animations in a \l Transition are run in parallel. To run them
208one after the other, use a SequentialAnimation, as shown below in \l {Grouping
209Animations}.
210
211See the \l Transition documentation for more information.
212
213
214\section1 Animation Elements
215
216To create an animation, choose from one of the built-in QML animation elements.
217While the above examples are demonstrated using PropertyAnimation, they could
218have used other elements depending on the type of the property to be animated
219and whether a single or multiple animations are required.
220
221All animation elements inherit from the \l Animation element. It is not
222possible to create \l Animation objects; instead, this element provides the
223essential properties and methods for animation elements. For example, it allows
224animations to be started and stopped through the \l {Animation::}{running}
225property and the \l{Animation::}{start()} and \l{Animation::}{stop()} methods.
226It can also define the number of \l {Animation::}{loops} for an animation.
227
228
229\section2 Property Animation Elements
230
231PropertyAnimation is the most basic animation element for animating a property.
232It 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,
234RotationAnimation and Vector3dAnimation: NumberAnimation provides a more
235efficient implementation for animating \c real and \c int properties, and
236Vector3dAnimation does the same for \c vector3d properties. ColorAnimation
237and RotationAnimation provide more specific attributes for animating color
238and rotation changes.
239
240A ColorAnimation allows color values for the \l {ColorAnimation::}{from}
241and \l {ColorAnimation::}{to} properties. The
242following animates the rectangle's \l {Rectangle::}{color} property:
243
244\snippet doc/src/snippets/declarative/animation-elements.qml color
245
246RotationAnimation allows a rotation's direction to be specified. The following
247animates the rectangle's \l {Item::rotation} property:
248
249\snippet doc/src/snippets/declarative/animation-elements.qml rotation
250
251In addition, the following specialized animation elements are available:
252
253\list
254\o SmoothedAnimation: a specialized NumberAnimation that provides smooth
255changes in animation when the target value changes
256\o SpringAnimation: provides a spring-like animation with specialized
257attributes 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
263See their respective documentation pages for more details.
264
265
266\section3 Easing
267
268Any PropertyAnimation-based animations can specify \l
269{PropertyAnimation::easing.type}{easing attributes} to control the
270easing curve applied when a property value is animated. These control the
271effect of the animation on the property value, to provide visual effects like
272bounce, acceleration and deceleration.
273
274For example, this modified version of an \l {Animations as Property Value
275Sources}{earlier example} uses \c Easing.OutBounce to create a bouncing effect
276when the animation reaches its target value:
277
278\snippet doc/src/snippets/declarative/animation-easing.qml 0
279
280The \l{declarative/animation/easing}{easing example} visually demonstrates each
281of the different easing types.
282
283\section2 Grouping Animations
284
285Multiple animations can be combined into a single animation using one of the
286animation group elements: ParallelAnimation or SequentialAnimation. As their
287names suggest, animations in a ParallelAnimation are run at the same time,
288while animations in a SequentialAnimation are run one after the other.
289
290To run multiple animations, define the animations within an animation group.
291The following example creates a SequentialAnimation that runs three animations
292one after the other: a NumberAnimation, a PauseAnimation and another
293NumberAnimation. The SequentialAnimation is applied as a \l{Animations as
294Property Value Sources}{property value source animation} on the image's \c y
295property, so that the animation starts as soon as the image is loaded, moving
296the image up and down:
297
298\snippet doc/src/snippets/declarative/animation-groups.qml 0
299\image propanim.gif
300
301Since the SequentialAnimation is applied to the \c y property, the individual
302animations within the group are automatically applied to the \c y property as
303well; it is not required to set their \l{PropertyAnimation::}{properties}
304values to a particular property.
305
306Animation groups can be nested. Here is a rather complex animation making use
307of both sequential and parallel animations:
308
309\snippet doc/src/snippets/declarative/animation-groups.qml 1
310
311Once individual animations are placed into a SequentialAnimation or
312ParallelAnimation, they can no longer be started and stopped independently. The
313sequential or parallel animation must be started and stopped as a group.
314
315See the \l {declarative/animation/basics}{Animation basics example} for a
316demonstration of creating and combining multiple animations in QML.
317
318
319
320\section2 Other Animation Elements
321
322In 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
327be used together with StateChangeScript to reused existing scripts
328\o PropertyAction: changes a property \e immediately during an animation,
329without animating the property change
330\endlist
331
332See their respective documentation pages for more details.
333
334*/
Note: See TracBrowser for help on using the repository browser.