Congratulations! You have finished the effect part. Now, let’s talk about something more interesting.
The Composition API provides a powerful animation engine which allows us to create quick and fluent animations with high performance. Developers can improve the user experiences with the help of Composition animations.
Generally, there are two types of animations: key frame animation and expression animation.
I’d like to start from key frame animation that we are all familiar with.
I think most of you have already familiar with key frame animation, especially for those who used to create animations using XAML Storyboard. In key frame animation, you let some properties change over time by inserting key frames. It’s exactly the same in Composition API.
var animation = _compositor.CreateScalarKeyFrameAnimation();
This snippet creates a key frame animation which change a value from 0 to 100 within 4 seconds.
Then, bind this animation to the property you want to change and start the animation.
But note that if you want to animate an effect, don’t forget to add the property you want to change to the “animatable properties” list while creating the effect.
var blurEffect = new GaussianBlurEffect
Unlike key frame animation, expression animation describes an animation with expressions, which is more like a reference based animation. The expression allows us to create a connection between the animation and other properties. In other words, for most cases, expression animation is driven by other properties instead of running independently. It can be driven by many different sources such as another animation and user input.
The official sample “Gear animation” shows us one of the most typical cases of using expression animation.
In this example, we have a couples of gears. Each gear is animated based on the animation of the gear preceding it, even if the preceding gear suddenly changes its speed or rotation direction. Obviously, a simple key frame animation can’t do this but expression animation provides us with a straightforward solution for this problem.
Before creating the expression animation, we should add an animation for the leading gear (red) using key frame animation.
var animation1 = _compositor.CreateScalarKeyFrameAnimation();
After that, we can create the animation for the rest of gears (gray) with the expression
ExpressionAnimation animation2 = _compositor.CreateExpressionAnimation("-visual1.RotationAngleInDegrees");
This expression describes an animation which changes the value with the rotation angle of
Then, we need to tell the animation engine what
visual1 refers to by setting the reference parameter.
Finally, apply animations to the target properties of
By doing so, the rotation angle of
visual2 will change with
visual1 while the rotation angle of
visual1 is determined by the key frame animation we defined just now (
To be continued…