Click here to Skip to main content
6,596,602 members and growing! (21,242 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » Howto     Intermediate

Apply Animated Effects to .NET Controls

By Xinjie ZHANG

Some animated effects may make your application more attractive.
C#, Windows, .NET CF, .NET, MobileVS.NET2003, Dev
Posted:7 May 2004
Views:59,939
Bookmarked:21 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 4.95 Rating: 3.87 out of 5

1
4 votes, 21.1%
2
3 votes, 15.8%
3
7 votes, 36.8%
4
5 votes, 26.3%
5

Sample Image - AnimatedEffects.gif

Introduction

Sometimes, a few lines of code could make an application more attractive. In this article, I would like to introduce a simple library to apply animated effects to .NET controls. But be careful, do not activate too many animated effects at the same time, it may cause your Pocket PC/Windows CE application to be slow.

Algorithm

Changing the location or/and dimension of a control periodically could present some interesting animated effects. All animated effects could be achieved by the following steps:

  1. Determine the initial and final locations/dimensions.
  2. Initialize the parameters, such as interval, steps count etc.
  3. Pre-calculate the locations/dimensions through the animation lifetime.
  4. Trigger a timer to start the animation.
  5. Change the location/dimension of a destination control to pre-calculated values whenever the timer is waked up.
  6. Once the animation states change, it should notify all listeners by events.

This article demonstrates three basic animated effects: move, zoom and rotate. For each effect, it includes three motions: uniform motion, accelerated motion and decelerated motion. The uniform motion is so easy to realize. So we just discuss a little about the accelerated motion, by the way the decelerated motion shares the same theory.

For the accelerated motion of a move animation, we suppose from the n*T moment to the (n+1)*T moment. The destination control moves from Pn to Pn+1, the distance between them should be: a*(n + 1)^2, where T is the period and a is a constant. The constant a is easy to be determined when the initial and final positions are given.

In this library, the traces of the three motions can be attained through a unified static method Procalculate():

protected static int[] Precalculate(int val1, int val2, 
        int steps, AnimatedMotions type)
{
    int [] vals = new int[steps];
    int sum = 0, currsum = 0, step, i;
    if (type == AnimatedMotions.UniformMotion) 
        sum = steps;
    else
        for (i = 1; i <= steps; i++) sum += i * i;
    for (i = 1; i < steps; i++)
    {
        step = (type == AnimatedMotions.DeceleratedMotion) 
               ? (steps + 1 - i) * (steps + 1 - i) : 
               (type == AnimatedMotions.UniformMotion ? 1 : i * i);
        currsum += step;
        vals[i - 1] = (val2 - val1) * currsum / sum + val1;
    }
    vals[steps - 1] = val2;
    return vals;
}

Using the code

There are four main classes in the library : AnimatedEffect, MoveAnimatedEffect, ZoomAnimatedEffect and RotateAnimatedEffect. The following class diagram gives you a clear view about them:

They are easy to use and also powerful enough to generate very complex animations. Let's look into them with four cases:

1. Simple Animation

If you would like to apply a simple animated effect to a control, two lines of codes are enough:

MoveAnimatedEffect moveEffect = new MoveAnimatedEffect();
moveEffect.Move(somecontrol, x1, y1, x2, y2, steps, 
           AnimatedMotions.AcceleratedMotion);

It causes a control, named somecontrol, to move from original position (x1,y1) to another position (x2,y2) with accelerated motion. You can rotate or zoom a control in the same way. Really simple, isn't it?

2. Cascaded Animation

In most cases, we want to make different controls be animated one by one according to our "play". At this time, cascaded animation should be helpful. The point is, once the previous animation stops, it throws out an event; a control program receives it and then actives the next animation. For example, we want to move a control from (x1, y1) to (x2, y2) and then rotate it. It may be realized like this:

MoveAnimatedEffect moveEffect = new MoveAnimatedEffect();
RotateAnimatedEffect rotateEffect = new RotateAnimatedEffect();

moveEffect.Move(somecontrol, x1, y1, x2, y2, 
  steps, AnimatedMotions.AcceleratedMotion);
moveEffect.StateChangedEvent += new 
  AnimationStateChangedEventHandler(NextAnimation);

private void NextAnimation(object sender)
{
    rotateEffect.RotateHorizontally(somecontrol, 
      somecontrol.Width, steps, 
      AnimatedMotions.UniformMotion);
}

3. Parallel Animation

The parallel animation allows to apply more than one animated effect to one control. It may generate more complex animations. For instance, we would like to move a control and zoom it at the same time. We can just insert the following code to the program:

MoveAnimatedEffect moveEffect = new MoveAnimatedEffect();
ZoomAnimatedEffect zoomEffect = new ZoomAnimatedEffect();

moveEffect.Move(somecontrol, x1, y1, x2, y2, 
  steps, AnimatedMotions.AcceleratedMotion);
zoomEffect.Zoom(somecontrol, somecontrol.Width, 0, 
  somecontrol.Height, 0, steps, 
  AnimatedMotions.UniformMotion);

4. More Sophisticated Animation

Applying the three animations I mentioned before, we could get more sophisticated animations. In this case, a simple draft would be helpful. Now, we would like to realize an animation play like this:

The figure above presents a sequence of animations. It includes four steps:

  • Step 1: Move the button "Exit" from top to bottom.
  • Step 2: Continue to move it from right to left.
  • Step 3: Move the button "Stop" from top to bottom.
  • Step 4: Display an image in the center of the screen and rotate it; four panels appear and move around it, at the same time, they zoom in/out themselves.

The following code may give you some points in realizing such an animation play:

step = 0;
moveEffect.Move(btnExit, 200, - 20, 200, 280, 
    30, AnimatedMotions.AcceleratedMotion); // Step 1

moveEffect.StateChangedEvent += new 
    AnimationStateChangedEventHandler(MainStateChanged);

private void MainStateChanged(object sender)
{
    step ++;
    switch (step)
    {
        case 1:        // Step 2

            moveEffect.Move(btnExit, 200, 280, 
              40, 280, 20, AnimatedMotions.DeceleratedMotion);
            break;
        case 2:        // Step 3

            btnControl.Visible = true;
            moveEffect.Move(btnControl, 200, - 20, 200, 
              280, 30, AnimatedMotions.AcceleratedMotion);
            break;
        case 3:        // Step 4

            labelMoveEffect.Stop();
            label1.Location = new Point(24, 250);
            pictureBox.Visible = true;
            rotateEffect.RotateHorizontally(pictureBox, 
              pictureBox.Width, 20, AnimatedMotions.AcceleratedMotion);
            rotateEffect.StateChangedEvent += new 
              AnimationStateChangedEventHandler(te_TurnOverEvent);
            ChangeImage();
            for (int i = 0; i < 4; i++)
            {
                panels[i].Visible = true;
                panelZoomEffects[i].Start();
            }
            Panels_MovingStopped(null);
            break;
    }
}

You can download the demo project to find the remaining code.

Conclusion

For a successful application, to be complete, scalable and stable is not all. We should also keep it in our minds: how to improve users' experiences. When our users have to face dozens of applications with the same interface, a little animation may make your application different and cheer your users. Why not try it?

History

You can always download the latest demos from OpenVue.net.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Xinjie ZHANG


Member
Xinjie ZHANG is a mobile solution developer. His expertise includes .NET/.NET CE Framework, ATL/WTL/MFC, J2ME, Hibernate+Spring Framework, Symbian SDK etc. Welcome to his XrossOne Studio.
Occupation: Web Developer
Location: China China

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
  • Pocket 1945 - A C# .NET CF Shooter
    An article on Pocket PC game development
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralGradient Pinmembersilkkeng15:41 4 Oct '05  
GeneralVery interesting - Can we get the regular version? Pinmemberzone51@walla.co.il22:07 9 May '05  
GeneralRe: Very interesting - Can we get the regular version? PinmemberXinjie ZHANG22:42 9 May '05  
QuestionWhere can I download the latest version Pinmemberaslim_sahal9:50 10 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 May 2004
Editor: Smitha Vijayan
Copyright 2004 by Xinjie ZHANG
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project