Click here to Skip to main content
15,889,909 members
Articles / Mobile Apps / Windows Phone 7
Tip/Trick

Images Animation By Using Story Board in Windows Phone 8/8.1

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
27 Sep 2014CPOL2 min read 32.6K   6   6
This tip is about making images animation for Windows Phone 8/8.1 using Storyboard class in C#.

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:

Cartoon Photos

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)

XML
<!-- XAML -->

<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">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- LOCALIZATION NOTE:
            To localize the displayed strings copy their values to appropriately named
            keys in the app's neutral language resource file (AppResources.resx) then
            replace the hard-coded text value between the attributes' quotation marks
            with the binding clause whose path points to that string name.

            For example:

                Text="{Binding Path=LocalizedResources.ApplicationTitle, 
                Source={StaticResource LocalizedStrings}}"

            This binding points to the template's string resource named "ApplicationTitle".

            Adding supported languages in the Project Properties tab will create a
            new resx file per language that can carry the translated values of your
            UI strings. The binding in these examples will cause the value of the
            attributes to be drawn from the .resx file that matches the
            CurrentUICulture of the app at run time.
         -->

        <!--TitlePanel contains the name of the application and page title-->

        <!--ContentPanel - place additional content here-->
        <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>

        <!--Uncomment to see an alignment grid to help ensure your controls are
            aligned on common boundaries.  The image has a top margin of -32px to
            account for the System Tray. Set this to 0 (or remove the margin altogether)
            if the System Tray is hidden.

            Before shipping remove this XAML and the image itself.-->
        <!--<Image Source="/Assets/AlignmentGrid.png" 
        VerticalAlignment="Top" Height="800" Width="480" 
        Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" 
        IsHitTestVisible="False" />-->
    </Grid>

</phone:PhoneApplicationPage>

Back End (C# Code)

C#
//C#
//the code is based on:http://compiledexperience.com/windows-phone/tutorials/candle/       
     private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
                     var storyboard = new Storyboard
            {
                RepeatBehavior = RepeatBehavior.Forever//animation will repeat after completion
            };

            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++)//16 is the number of images that are going to be displayed
            {
                var keyframe = new DiscreteObjectKeyFrame
                {
                    KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(50 * i)),//Time Interval
                    Value = String.Format("/Images/win_"+i.ToString()+".png")//Source of images
                };

                animation.KeyFrames.Add(keyframe);
            }

            Resources.Add("Storyboard", storyboard);//Add story board to resources

            storyboard.Begin();
        
        }

Some snap shots of the app are given below:

Image 1image 2Image 4

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

  • First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCode not working for Windows Phone 8.1 Run time (store) app Pin
Harshal180430-Dec-14 23:05
Harshal180430-Dec-14 23:05 
AnswerRe: Code not working for Windows Phone 8.1 Run time (store) app Pin
Muhammad Muddasar Yamin31-Dec-14 4:04
professionalMuhammad Muddasar Yamin31-Dec-14 4:04 
Questionhow to display round corner image Pin
hirawadinesh28-Sep-14 19:03
hirawadinesh28-Sep-14 19:03 
AnswerRe: how to display round corner image Pin
Muhammad Muddasar Yamin28-Sep-14 21:43
professionalMuhammad Muddasar Yamin28-Sep-14 21:43 
BugImages not showing Pin
Serge Desmedt28-Sep-14 1:38
professionalSerge Desmedt28-Sep-14 1:38 
GeneralRe: Images not showing Pin
Muhammad Muddasar Yamin28-Sep-14 2:37
professionalMuhammad Muddasar Yamin28-Sep-14 2:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.