How to Create Animation in WPF?

 In some instances we need some animation to be shown in the User interface to improve the User Experience for example showing the progress bar indicating the live progress of some activity which is ongoing in the background.

What is Animation?

Animation is the process of changing the properties of an image/text such that it appears as moving in Realtime. Animation helps to improve the overall user experience. In modern application we can see some kind of animation for every event which is happening in the application.

Implementing Animation in WPF

In this post lets understand how animation works in WPF with the help of Storyboard. Here we are going to discuss about basic animation like performing rotation of some shapes.

Below code demonstrates the animation for the Spinner Control which is used to indicate that some background task is in progress.

XAML Code

Animation using Storyboard WPF

In the above code we have taken an ellipse and made it into a circle with equal width and height. Since we are going to make a basic spinner control we need to cut a small portion in the circle. This is accomplished with the help of Stroke dash array where some percent of the circle is left blank.

Now coming to the main part related to animation. Simply put animation is basically achieved by changing some visible property of an object in a repeated manner. This is achieved with the help of DoubleAnimation which does the task of changing the specified property and the Storyboard stores this animation information.

Next is there should be some one which should do the job of rendering the object whenever the property changes by calculating the associated render matrix. This is achieved with the help of RenderTransform which transforms the rendered object.

Next there should be some trigger which should start the entire process. This is achieved using Triggers. In our case we have used the EventTrigger as Loaded that is whenever the Control is loaded the Storyboard should execute. Inside the storyboard we have changed the Angle Property of the Rotate Transform which makes our ellipse control to rotate starting from Angle=0 to Angle=360. The RepeatBehaviour tells the animation duration. In our case we have set it to Forever which means the animation should continue until the application closes.

There are various other properties which can be targeted for animation like scaling of control in X and Y directions, Skew etc.

Below video shows the above code in action:



 

Comments

Popular posts from this blog

Creating RESTful Minimal WebAPI in .Net 6 in an Easy Manner! | FastEndpoints

Mastering Concurrency with Latches and Barriers in C++20: A Practical Guide for Students

Graph Visualization using MSAGL with Examples