Silverlight Tip: Dynamic animations - DevCorner

Silverlight Tip: Dynamic animations

Hello,

I was asked about creation of animations dynamically for various elements. Today I’ll show how to create such animations for any (almost) Silverlight element. I’ll create some of popular standard PowerPoint animations.

I know that it is not the full set of animations available in PowerPoint, also the animations itself are probably not perfect but it is a good starting point to those who want to know how to do it.

Generally, to create the animation on-the-fly we need to create a storyboard, add one or more animations to it, add the created storyboard to resources of our container and run it.

For this sample I’ve created function, which will receive Target Element, new storyboard name and some animation definitions. The function will add created storyboard to topmost parent element resources.

private void CreateAnimation(FrameworkElement targetElement,
                             string StoryboardName,
                             AnimationSpeed StoryboardDuration,
                             AnimationType type,
                             AnimationDirection direction)
{
    //...
}

Also, I’ve created a couple Enums to define animation

private enum AnimationType
    {
      FlyIn,
      FlyOut,
      FadeIn,
      FadeOut
    }
private enum AnimationDirection
   {
     Top,
     Bottom,
     Right,
     Left,
     TopLeft,
     TopRight,
     BottomLeft,
     BottomRight,
     None
   }
private enum AnimationSpeed
   {
     ExtremlySlow = 10000,
     VerySlow = 5000,
     Slow = 3000,
     Normal = 1000,
     Fast = 500,
     VeryFast = 250,
     ExtrimlyFast = 100
   }

In my “CreateAnimation” function I did the following:

1. Prepared TransformGroup with all possible Transforms for my desired element

private FrameworkElement prepareTransformGroup(FrameworkElement targetElement, Point transformOrigin)
{
  targetElement.RenderTransform = new TransformGroup();
  (targetElement.RenderTransform as TransformGroup).Children.Add(new ScaleTransform());
  (targetElement.RenderTransform as TransformGroup).Children.Add(new SkewTransform());
  (targetElement.RenderTransform as TransformGroup).Children.Add(new RotateTransform());
  (targetElement.RenderTransform as TransformGroup).Children.Add(new TranslateTransform());
  targetElement.RenderTransformOrigin = transformOrigin;
 
  return targetElement;
}

2. Created new Duration object (based on received parameter) and Storyboard object

Duration duration = new Duration(TimeSpan.FromMilliseconds((double)StoryboardDuration));
Storyboard sb = new Storyboard();
sb.Duration = duration;

3. Then, based on direction, created animations (will see one of them – in my case other will be almost identical)

DoubleAnimation animX = new DoubleAnimation();
animX.Duration = duration;
 
FrameworkElement parent = GetParent(targetElement);
if (type == AnimationType.FlyIn)
  animX.From = parent.ActualWidth * -SCALE_FACTOR;
else
  animX.To = parent.ActualWidth * -SCALE_FACTOR;
animX.SetValue(Storyboard.TargetNameProperty, targetElement.Name);
Storyboard.SetTargetProperty(animX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));
sb.Children.Add(animX);
 
DoubleAnimation animY = new DoubleAnimation();
animY.Duration = duration;
if (type == AnimationType.FlyIn)
  animY.From = parent.ActualHeight * -SCALE_FACTOR;
else
  animY.To = parent.ActualHeight * -SCALE_FACTOR;
animY.SetValue(Storyboard.TargetNameProperty, targetElement.Name);
Storyboard.SetTargetProperty(animY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"));
sb.Children.Add(animY);

4. Added created storyboard to topmost parent

FrameworkElement p = GetParent(targetElement);
p.Resources.Add(StoryboardName, sb);

5.GetParent function recursively traverses the visual tree until it gets topmost FrameworkElement

private FrameworkElement GetParent(FrameworkElement element)
{
  if ((element.Parent as FrameworkElement) == null)
    return element;
  else
    return GetParent((element.Parent as FrameworkElement));
}

Now we can call the “CreateAnimation” and then execute created animation

FrameworkElement elm = someButton;
if (null == ((LayoutRoot.Parent as FrameworkElement).Resources["myDynamicAnimation"] as Storyboard))
    CreateAnimation(elm, "myDynamicAnimation", AnimationSpeed.Fast, AnimationType.FadeIn, AnimationDirection.None);
((LayoutRoot.Parent as FrameworkElement).Resources["myDynamicAnimation"] as Storyboard).Begin();

Here it works…

Dynamic Silverlight Animation

Full sources here.

 

Enjoy,

Alex

Published Tuesday, January 06, 2009 2:19 PM by Alex Golesh

Comments

# Silverlight Cream for December 06, 2009 -- #477

In this issue: James Bacon, Chris Anderson, Jesse Liberty, Expression Blend and Design Blog, Laurent

Tuesday, January 06, 2009 11:43 PM by Community Blogs

# re: Silverlight Tip: Dynamic animations

How is this any better than a usual Tweener library again?

I agree it's awesome that you built this, got basic tweening experience and shared this around.

I'm just a bit concerned someone will pick up this V0.1 library and use it when Silverlight Open source offers mature Tweening solutions.

-- Justin

Microsoft Siverlight Program Manager

Wednesday, January 07, 2009 10:25 AM by Justin-Josef Angel [MVP]

# re: Silverlight Tip: Dynamic animations

Justin,

I never tried to build another Tweener library... All I did is simple demostration of dynamic animation creation... The fact, that I choosed to produce some powerpoint-like animations is because of simpleness...

Regards,

Alex

Wednesday, January 07, 2009 11:54 AM by Alex Golesh

# re: Silverlight Tip: Dynamic animations

Excellent site, keep up the good work

Monday, September 07, 2009 1:51 AM by Work from home

# re: Silverlight Tip: Dynamic animations

Hey good stuff...keep up the good work! :)

Monday, September 07, 2009 8:11 PM by Bill Bartmann

# re: Silverlight Tip: Dynamic animations

This site rocks!

Wednesday, September 09, 2009 8:23 PM by Bill Bartmann

# re: Silverlight Tip: Dynamic animations

Excellent site, keep up the good work

Friday, September 11, 2009 3:31 AM by Bill Bartmann_

# re: Silverlight Tip: Dynamic animations

Great site...keep up the good work. I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I'm glad I found your blog.  Thanks,

A definite great read...

- Bill Bartmann

Sunday, September 13, 2009 9:17 PM by Bill Bartmann

# re: Silverlight Tip: Dynamic animations

Excellent site, keep up the good work.  I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I'm glad I found your blog.  Thanks,

A definite great read...

<a href="forum.dotnetpanel.com/.../a>

Wednesday, September 16, 2009 5:52 PM by Bill Bartmann

# re: Silverlight Tip: Dynamic animations

Super site....where's the blog roll?

Saturday, September 26, 2009 7:54 PM by trade stocks online

# re: Silverlight Tip: Dynamic animations

Generally I do not post on blogs, but I would like to say that this post really forced me to do so, Excellent post!

Monday, January 11, 2010 12:57 AM by online stock trading advice

# re: Silverlight Tip: Dynamic animations

Just wanted to say that I read your blog quite frequently and I'm always amazed at some of the stuff people post here.  

But keep up the good work, it's always interesting.

See ya,

Tuesday, January 19, 2010 4:46 PM by Robert Shumake

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: