Posted by: Zeeshan Amjad | October 28, 2009

Multiple Animation effects simultaneously


We have seen one example to perform the animation with C# code. Now we are going to extend our study and perform more than one animation simultaneously. This time we are going to see the animation of Color. Here is a partial class diagram of ColorAnimatinBase class.

ColorAnimationBase

Here is the usage of ColorAnimation class.

  1: SolidColorBrush scb = new SolidColorBrush(Colors.Blue);
  2: 
  3: ColorAnimation ca = new ColorAnimation();
  4: ca.From = Colors.Blue;
  5: ca.To = Colors.Red;
  6: ca.AutoReverse = true;
  7: ca.Duration = TimeSpan.FromSeconds(5);
  8: ca.RepeatBehavior = RepeatBehavior.Forever;
  9: scb.BeginAnimation(SolidColorBrush.ColorProperty, ca);
 10: tb.Foreground = scb;
 11: 

Also note that there are only two children of ColorAnimationBase class, however DoubleAnimationBase class has three children.

The usage of Color animation is little bit different than double animation. Here we first create the object of SolidColorBrush then use ColorAnimation class with that brush and assign it to the control where we want to apply this color animation.

If we want to use both animation together then we can apply both animation. Here is a complete C# code of applying more than one animation.

  1: using System;
  2: using System.Windows;
  3: using System.Windows.Controls;
  4: using System.Windows.Media;
  5: using System.Windows.Media.Animation;
  6: 
  7: public class MyWindow : Window
  8: {
  9:     public MyWindow()
 10:     {
 11:         Title = "Animation with Code";
 12:         Width = 400;
 13:         Height = 300;
 14: 
 15:         TextBlock tb = new TextBlock();
 16:         tb.Text = "Hello Animation World";
 17:         tb.Foreground = Brushes.Blue;        
 18: 
 19:         Content = tb;
 20: 
 21:         SolidColorBrush scb = new SolidColorBrush(Colors.Blue);
 22: 
 23:         ColorAnimation ca = new ColorAnimation();
 24:         ca.From = Colors.Blue;
 25:         ca.To = Colors.Red;
 26:         ca.AutoReverse = true;
 27:         ca.Duration = TimeSpan.FromSeconds(5);
 28:         ca.RepeatBehavior = RepeatBehavior.Forever;
 29:         scb.BeginAnimation(SolidColorBrush.ColorProperty, ca);
 30:         tb.Foreground = scb;
 31: 
 32:         DoubleAnimation da = new DoubleAnimation();
 33:         da.From = 10;
 34:         da.To = 32;
 35:         da.AutoReverse = true;
 36:         da.Duration = TimeSpan.FromSeconds(10);
 37:         da.RepeatBehavior = RepeatBehavior.Forever;
 38:         tb.BeginAnimation(TextBlock.FontSizeProperty, da);     
 39:     }
 40: }
 41: 
 42: public class AnimatinClass
 43: {
 44:     [STAThread]
 45:     public static void Main()
 46:     {
 47:         MyWindow win = new MyWindow();
 48: 
 49:         Application app = new Application();
 50:         app.Run(win);
 51:     }
 52: }
 53: 

This program display one string “Hello Animation World” in font size 10 and continuously increase its font size and change its color from Blue to read and then back to Blue. Here is one of the output of the program.

ColorAnimation

Advertisements

Responses

  1. […] is double and WPF already has DoubleAnimation class. We even did the same thing with color property here. But if we want to do the same thing with brush then WPF doesn’t provide any BrushAnimation […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: