Introduction
Animation can enhance Smart Phone/Tabs apps by adding interactivity, movement and rich user experience. Microsoft understands this, therefore they have added Storyboard
class in their new development environment which provides basic as well as advanced animation functionality. Quick starter for animation in Windows can be viewed in the following link:
This tip deals with Storyboard
class in C# whose details can be viewed in the following link:
Background
The application is developed to replicate the functionality of an IOS app presented in a tutorial at
http://www.appcoda.com/ios-programming-animation-uiimageview/.
In the above tutorial UIImage
view, an IOS is used to display an animation of images using animation methods in Windows Phone 8/8.1 .Same results can be achieved by Storyboard
. The idea behind the animation is moving different images at fast pace so it looks like an animation. The images which I am going to use are from app code. A snapshot of them is given below:
Using the Code
The code is written in Visual Studio 2013. There are two sections in the code, one is the XAML (Design View) and the other is in C# (Back end logic). In the design page, only an image view is added with the name of "ImageView1
" to display the images. In the backend Storyboard
, animation is created on load event of the application page.
Front End (XAML Code)
-->
<phone:PhoneApplicationPage
x:Class="SimpleAnimation.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">
-->
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
-->
-->
-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image x:Name="ImageView1" HorizontalAlignment="Left"
Height="758" VerticalAlignment="Top" Width="446" Stretch="Fill"/>
</Grid>
-->
-->
</Grid>
</phone:PhoneApplicationPage>
Back End (C# Code)
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
var storyboard = new Storyboard
{
RepeatBehavior = RepeatBehavior.Forever };
var animation = new ObjectAnimationUsingKeyFrames();
Storyboard.SetTarget(animation, ImageView1);
Storyboard.SetTargetProperty(animation, new PropertyPath("Source"));
storyboard.Children.Add(animation);
for (int i = 1; i <= 16; i++) {
var keyframe = new DiscreteObjectKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(50 * i)), Value = String.Format("/Images/win_"+i.ToString()+".png") };
animation.KeyFrames.Add(keyframe);
}
Resources.Add("Storyboard", storyboard);
storyboard.Begin();
}
Some snap shots of the app are given below:
It is not complicated to create an animation. This is a simple animation, but if you want to create more complex animations and even combine animations, you can find more information on the web.
Points of Interest
This Storyboarding can be used for building simple 2D games by altering the animation images at predefined events occurred during game play.
History