How To: Play a Cyclic Animation |
This topic explains how to cycle (loop) an existing animation.
Most animations, such a FromToByAnimationT or KeyFrameAnimationT, do not automatically support cycling (looping). If these types of animations should be looped, an AnimationClipT needs to be used:
// Create a simple color animation. ColorKeyFrameAnimation animation = new ColorKeyFrameAnimation colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(0), Color.Red)); colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(1), Color.Green)); colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(3), Color.Blue)); colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(4), Color.Red)); // Use an AnimationClip<T> to turn the 4 second colorAnimation into // an animation that loops forever. _cyclicAnimation = new AnimationClip<Color>(animation) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, };
Note that the first key frame and the last key frame ("loop key") should have the identical values in order to achieve smooth transitions between cycles.
An AnimationClipT can be used to cycle animations that implement IAnimationT. A TimelineClip is a more general variant that can be used to cycle anything that implements ITimeline.