WPF动画的最佳实践 [英] Wpf Animation Best Practices

查看:190
本文介绍了WPF动画的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找关于WPF动画的最佳做法或准则。关于性能方面的考虑尤其是信息。

I am looking for any best practices or guidelines regarding animation in WPF. Especially information regarding performance considerations.

推荐答案

在我在这个领域获得了更多的经验,因为我也问过这个问题,我会回答这个自己。我的经验来自于WPF心灵地图应用工作 NovaMind - 我们已经做了很多的动画近来我们的$p$psenter功能: - )

After I have gained a bit more experience in this field since I have asked the question, I will answer this myself. My experience comes from working on the WPF mind mapping application NovaMind - We have done lots of animations lately for our Presenter feature in NovaMind Platinum :-)

在优化WPF应用程序性能MSDN部分大约有总体考虑一些有用的信息写WPF应用程序时:

The MSDN section on Optimizing WPF Application Performance has some useful information about general considerations when writing WPF apps:

<一个href=\"http://msdn.microsoft.com/en-us/library/aa970683.aspx\">http://msdn.microsoft.com/en-us/library/aa970683.aspx

下面是我发现特别有用,并涉及到一些动画位:

Here are some bits which I have found especially useful and are related to animation:


  • 该CompositionTarget.Rendering事件导致WPF连续动画。如果您使用此事件,在每一个机会分离。

  • The CompositionTarget.Rendering event causes WPF to continuously animate. If you use this event, detach it at every opportunity.

当您使用画笔设置元素的填充或描边,最好是设置Brush.Opacity值,而不是设置元素的Opacity属性。修改元素的Opacity属性会导致WPF创建临时表。

When you use a Brush to set the Fill or Stroke of an element, it is better to set the Brush.Opacity value rather than the setting the element's Opacity property. Modifying an element's Opacity property can cause WPF to create a temporary surface.

您可能能够更新改造,而不是取代它作为一个RenderTransform属性的值。这是在涉及动画场景尤其如此。通过更新现有的变换,避免引发不必要的布局计算。

You may be able to update a Transform rather than replacing it as the value of a RenderTransform property. This is particularly true in scenarios that involve animation. By updating an existing Transform, you avoid initiating an unnecessary layout calculation.

下面是我通过摸索掌握:

Here is what I have learned through trial and error:


  1. 我说,你有几个要素与影响,如BlurEffect应用。它的办法更快的效果应用于这些元素,而不是元素本身的容器。虽然效果硬件加速,WPF似乎并不善于处理一些小件物品有效果。 - 如果不希望同样的模糊半径,并且可以施加不群它们与效果的容器,它实际上更快的元件渲染为位图(软件),然后围绕动画位(如果这是可能的) 。有效果(或不透明度为此事)上的对象时,动画快速杀死性能。

  2. 设置就刷不透明度,而不是元素(如进一步如上所述)动画对象时,使一个巨大的性能差异。

  3. 保持视觉的数量下降。动画大量粒子是困难的,即使是与上述提示。在这种情况下,您可能需要恢复而不是WriteableBitmap的。

  1. Say, you have a couple of elements with a Effect such as the BlurEffect applied. It is way faster to apply the effect to the container of these elements rather than the elements themselves. Even though the effect is hardware accelerated, WPF doesn't seem to be good at handling a number of small items with effects. - If you don't want the same blur radius and cannot group them in a container with the effect applied, it is actually faster to render the element to a bitmap (in software) and then animate the bitmap around (if that is possible). Having effects (or opacity for that matter) on objects quickly kills performance when animating.
  2. Setting the opacity on the brush rather than the element (as mentioned further above) makes a huge performance difference when animating objects.
  3. Keep the number of visuals down. Animating a large number of particles is difficult, even with the above tips. In this case you may need to revert to WriteableBitmap instead.

我也有听说,这是更快的渲染许多小对象由容器覆盖的OnRender,然后使用的DrawingContext渲染他们,而不是他们直接增加了可视化树。实际上,这并没有使我的情况有什么区别(渲染约300椭圆形几何图形时),但它可能在某些情况下有所帮助。这个理论听起来很坚实的。

I have also heard that it is faster to render many little objects by overriding OnRender in the container and then rendering them using the drawingContext rather than adding them to the visual tree directly. In practice this didn't make any difference in my scenario (when rendering around 300 ellipse geometries) but it might be helpful in some scenarios. The theory sounds solid.

最后,我发现内置到WPF的方式太麻烦了动画类和动画使用图书馆的弱旅有更多的乐趣和成功:的人工制品动画的。说真的,试试看。 (它也可用于Silverlight的),这是动画(在code)应该已经等。

Finally, I have found the animation classes built into WPF way too cumbersome and had much more fun and success using the underdog of animation libraries: Artefact Animator. Seriously, give it a try. (it is also available for Silverlight) It is what animating (in code) should have been like.

所有不是彩虹和独角兽虽然。我仍然觉得它不可能在更高的分辨率运行,全屏模式下创造真正流畅的动画。更多的是我的问题如何知道为什么动画口吃? - 我想AP preciate上任何输入

All is not rainbows and unicorns though. I still find it impossible to create truly fluid animations when running full screen in higher resolutions. More on that in my question How to know why an animation stutters? - I would appreciate any input on that.

喝彩,祝你好运,如果你有一些很酷的东西来展示,让我知道:)

cheers, good luck and if you have something cool to show, let me know :)

这篇关于WPF动画的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆