Basic animation in Qt and WP7
vdharankar
(Talk | contribs) m (Vdharankar - - →WP7 Project (MainPage.xaml)) |
hamishwillee
(Talk | contribs) m (Hamishwillee - Addition to article of: Category:Windows Phone 7.5) |
||
| (5 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
| − | [[Category:Windows Phone]][[Category:Code | + | [[Category:Windows Phone]][[Category:Code Examples]][[Category:Porting]][[Category:Qt Quick]][[Category:MeeGo Harmattan]][[Category:Symbian]][[Category:Animation]] |
| − | {{Abstract|This article demonstrates how | + | {{Abstract|This article demonstrates how to create basic animation in Qt and WP7. For a more detailed explanation of animation on each framework see the topics [http://doc.qt.nokia.com/4.7/qdeclarativeanimation.html QML Animation and Transitions] and [http://msdn.microsoft.com/en-us/library/cc189019(v=vs.96).aspx Windows Phone Animation Overview], respectively.}} |
| − | + | {{SeeAlso| | |
| − | {{ArticleMetaData | + | * [http://doc.qt.nokia.com/4.7/qdeclarativeanimation.html QML Animation and Transitions] |
| − | |sourcecode= [[Media: FadingAnimationQt.zip]] [[Media: FadingAnimationWP7.zip]] | + | * [http://msdn.microsoft.com/en-us/library/cc189019(v=vs.96).aspx Windows Phone Animation Overview] (msdn) |
| + | }} | ||
| + | {{ArticleMetaData <!-- v1.2 --> | ||
| + | |sourcecode= [[Media: FadingAnimationQt.zip]] [[Media: FadingAnimationWP7.zip]] | ||
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | ||
|devices= WP7 Emulator <!-- Devices tested against - e.g. ''devices=N95, N8'') --> | |devices= WP7 Emulator <!-- Devices tested against - e.g. ''devices=N95, N8'') --> | ||
| Line 9: | Line 12: | ||
|platform= WP7.1 <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later --> | |platform= WP7.1 <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later --> | ||
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) --> | |devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) --> | ||
| − | |signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer --> | + | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> |
| − | |capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. --> | + | |signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer --> |
| + | |capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. --> | ||
|keywords= Animation <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase --> | |keywords= Animation <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase --> | ||
| − | |id= <!-- | + | |language= <!-- Language category code for non-English topics - e.g. Lang-Chinese --> |
| + | |translated-by= <!-- [[User:XXXX]] --> | ||
| + | |translated-from-title= <!-- Title only --> | ||
| + | |translated-from-id= <!-- Id of translated revision --> | ||
| + | |review-by= <!-- After re-review: [[User:username]] --> | ||
| + | |review-timestamp= <!-- After re-review: YYYYMMDD --> | ||
| + | |update-by= <!-- After significant update: [[User:username]]--> | ||
| + | |update-timestamp= <!-- After significant update: YYYYMMDD --> | ||
|creationdate= 25th October,2011<!-- Format YYYYMMDD --> | |creationdate= 25th October,2011<!-- Format YYYYMMDD --> | ||
| − | |author= [[User: | + | |author= [[User:Somnathbanik]] |
}} | }} | ||
==Introduction== | ==Introduction== | ||
| − | + | The animation frameworks on Qt and Windows Phone are conceptually similar. To create an animation we specify a property to change and (possibly) its end points, and the animation framework(s) smoothly animates the property (and UI) according to some specified easing curve. | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| + | This example shows how to fade an image on Qt and WP7 by animating its opacity. For Qt we use the [http://doc.qt.nokia.com/4.7/qml-propertyanimation.html PropertyAnimation] and for WP7 we use [http://msdn.microsoft.com/en-us/library/system.windows.media.animation.doubleanimation%28v=VS.96%29.aspx DoubleAnimation] to create similar effect. In both cases the animations will cycle continuously. | ||
| + | <gallery widths=200px heights=400px> | ||
| + | File: FadingAnimationQt.png|Fading Animation in Qt | ||
| + | File: FadingAnimationWP7.png|Fading Animation in WP7 | ||
| + | </gallery> | ||
==Implementation== | ==Implementation== | ||
| − | + | First create an empty project for both Qt and WP7. The next section shows how to create the animation example on Qt. The following section then implements the same functionality on WP7. | |
===Qt Project (main.qml)=== | ===Qt Project (main.qml)=== | ||
| − | We take an image as our | + | We take an image as our object to be animated: |
| − | <code | + | <code css> |
Image { | Image { | ||
id: imageAnimation | id: imageAnimation | ||
| Line 49: | Line 48: | ||
} | } | ||
</code> | </code> | ||
| − | |||
| − | + | To animate the opacity change we use a sequential animation. This animation first runs a [http://doc.qt.nokia.com/4.7/qml-pauseanimation.html PauseAnimation] with duration of 1000 milliseconds and then a [http://doc.qt.nokia.com/4.7/qml-propertyanimation.html PropertyAnimation], changing the {{Icode|opacity}} of the image to 0. To create a continuous animation we set the animation loop type to {{Icode|infinite}}. | |
| + | <code css> | ||
SequentialAnimation { | SequentialAnimation { | ||
id: animation | id: animation | ||
| Line 65: | Line 64: | ||
} | } | ||
</code> | </code> | ||
| − | + | We call the {{Icode|start()}} method to start the animation once the component has been created. | |
| − | <code | + | <code javascript> |
Component.onCompleted: animation.start(); | Component.onCompleted: animation.start(); | ||
</code> | </code> | ||
| + | |||
===WP7 Project (MainPage.xaml) === | ===WP7 Project (MainPage.xaml) === | ||
| − | In WP7 we use [http://msdn.microsoft.com/en-us/library/system.windows.media.animation.doubleanimation%28v=VS.96%29.aspx DoubleAnimation] | + | In WP7 we use [http://msdn.microsoft.com/en-us/library/system.windows.media.animation.doubleanimation%28v=VS.96%29.aspx DoubleAnimation] inside a StoryBoard. The animation object is set to an image and the property {{Icode|Opacity}} is changed from '''1.0''' to '''0.0''' over a specified duration. We also set this animation to infinite using the {{Icode|RepeatBehavior}} property. {{Icode|AutoReverse}} property reverses the animation property, so when the {{Icode|Opacity}} is '''0.0''' it brings back to '''1.0'''. |
<code xml> | <code xml> | ||
<StackPanel> | <StackPanel> | ||
| Line 83: | Line 83: | ||
</StackPanel.Resources> | </StackPanel.Resources> | ||
<TextBlock Text="Fade In/Out Animation" FontSize="35" FontFamily="Times New Roman" Foreground="Red" Margin="70,100,0,0" /> | <TextBlock Text="Fade In/Out Animation" FontSize="35" FontFamily="Times New Roman" Foreground="Red" Margin="70,100,0,0" /> | ||
| − | <Image | + | <Image Margin="0,100,0,0" Height="200" Width="300" Source="Thumbnails/1.jpeg" Loaded="Start_Animation" x:Name="MyAnimatedImage"/> |
| − | + | ||
| − | + | ||
</StackPanel> | </StackPanel> | ||
| − | |||
| − | |||
</code> | </code> | ||
{{Icode|Loaded}} property start the animation by calling the method {{Icode|Start_Animation()}} | {{Icode|Loaded}} property start the animation by calling the method {{Icode|Start_Animation()}} | ||
| − | <code | + | <code csharp> |
| − | + | ||
// Start the animation when the object loads. | // Start the animation when the object loads. | ||
private void Start_Animation(object sender, EventArgs e) | private void Start_Animation(object sender, EventArgs e) | ||
| − | { | + | { |
| − | + | ||
myAnimation.Begin(); | myAnimation.Begin(); | ||
} | } | ||
| − | |||
| − | |||
</code> | </code> | ||
==Source Code== | ==Source Code== | ||
| − | *The full source code | + | *The full source code of Qt example is available here: [[File: FadingAnimationQt.zip]] |
| − | *The full source code of | + | *The full source code of WP7 example is available here: [[File: FadingAnimationWP7.zip]][[Category:Windows Phone 7.5]] |
Latest revision as of 08:14, 30 November 2012
This article demonstrates how to create basic animation in Qt and WP7. For a more detailed explanation of animation on each framework see the topics QML Animation and Transitions and Windows Phone Animation Overview, respectively.
See Also
Article Metadata
Code Example
Tested with
Compatibility
Article
Contents |
Introduction
The animation frameworks on Qt and Windows Phone are conceptually similar. To create an animation we specify a property to change and (possibly) its end points, and the animation framework(s) smoothly animates the property (and UI) according to some specified easing curve.
This example shows how to fade an image on Qt and WP7 by animating its opacity. For Qt we use the PropertyAnimation and for WP7 we use DoubleAnimation to create similar effect. In both cases the animations will cycle continuously.
Implementation
First create an empty project for both Qt and WP7. The next section shows how to create the animation example on Qt. The following section then implements the same functionality on WP7.
Qt Project (main.qml)
We take an image as our object to be animated:
Image {
id: imageAnimation
anchors.fill: parent
fillMode: Image.PreserveAspectCrop
source: "Thumbnails/1.jpeg"
}
To animate the opacity change we use a sequential animation. This animation first runs a PauseAnimation with duration of 1000 milliseconds and then a PropertyAnimation, changing the opacity of the image to 0. To create a continuous animation we set the animation loop type to infinite.
SequentialAnimation {
id: animation
PauseAnimation { duration: 1000 }
PropertyAnimation {
target: imageAnimation
loops: Animation.Infinite
properties: "opacity"
duration: 800
to: 0
}
}
We call the start() method to start the animation once the component has been created.
Component.onCompleted: animation.start();
WP7 Project (MainPage.xaml)
In WP7 we use DoubleAnimation inside a StoryBoard. The animation object is set to an image and the property Opacity is changed from 1.0 to 0.0 over a specified duration. We also set this animation to infinite using the RepeatBehavior property. AutoReverse property reverses the animation property, so when the Opacity is 0.0 it brings back to 1.0.
<StackPanel>
<StackPanel.Resources>
<Storyboard x:Name="myAnimation">
<DoubleAnimation
Storyboard.TargetName="MyAnimatedImage"
Storyboard.TargetProperty="Opacity"
From="1.0" To="0.0" Duration="0:0:5"
AutoReverse="True" RepeatBehavior="Forever" />
</Storyboard>
</StackPanel.Resources>
<TextBlock Text="Fade In/Out Animation" FontSize="35" FontFamily="Times New Roman" Foreground="Red" Margin="70,100,0,0" />
<Image Margin="0,100,0,0" Height="200" Width="300" Source="Thumbnails/1.jpeg" Loaded="Start_Animation" x:Name="MyAnimatedImage"/>
</StackPanel>
Loaded property start the animation by calling the method Start_Animation()
// Start the animation when the object loads.
private void Start_Animation(object sender, EventArgs e)
{
myAnimation.Begin();
}
Source Code
- The full source code of Qt example is available here: File:FadingAnimationQt.zip
- The full source code of WP7 example is available here: File:FadingAnimationWP7.zip

