Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Tuesday, July 31, 2012

How to control the frame rate for WPF animations

Animations are very cool, but they can cause a high CPU load. One reason could be a missing hardware acceleration due to a old graphics adapter or a software rendering constraint. Another reason could be the the high frame rate of animations that is set to 60 fps by default.
You can easily lower the framerate for all animations by overriding the DesiredFrameRate property of the timeline. Just add the following code to your project and play around with the setting to find a good tradeoff between performance and aesthetic.
 
Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline),
   new FrameworkPropertyMetadata { DefaultValue = 30 });
 
 
Or you can set the framerate individually for each animation in XAML, using the following code:
 
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0.5" 
                 From="1.0" To="0.5" Timeline.DesiredFrameRate="30" />

No comments:

Post a Comment