这里就是我会做,以提高性能:
1)创建仅是在后台可见的矩形。
2)将每个Rectangle的Fill属性随机绑定到预定义颜色的一个StaticResources(10-20应该可以做到这一点)。
3)创建一个动画这些静态资源的故事板。
你会失去每个矩形的个性,但它应该动画而不会杀死你的应用程序。
编辑 - 代码示例
例如,添加一些资源以动画(他们是矩形,因为你不能直接动画化的SolidColorBrush):
<Window.Resources>
<Rectangle x:Key="Color0" Fill="#FFFFCFFF" />
<Rectangle x:Key="Color1" Fill="#FFFFC2C2" />
<Rectangle x:Key="Color2" Fill="#FFFFEFD2" />
...
</Window.Resources>
你的故事板看起来会像:
<Storyboard x:Key="BackgroundAnimation" AutoReverse="True" RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames Storyboard.Target="{StaticResource Color0}"
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
BeginTime="0:0:0:0"
AutoReverse="True"
Duration="00:00:03.00">
<ColorKeyFrameCollection>
<EasingColorKeyFrame Value="Transparent" />
</ColorKeyFrameCollection>
</ColorAnimationUsingKeyFrames>
<!-- Add keyframes for each color, varying start and duration -->
...
</Storyboard>
在你的代码隐藏你产生所有的矩形,你需要绑定到资源。因此,在循环的某个地方,您需要添加:
// I'll leave the implementation of GetRandomColorId to you
resourceId = GetRandomColorId(MAX_COLORS);
Shape source = (Shape)this.FindResource("Color" + resourceId);
Binding binding = new Binding
{
Path = new PropertyPath("Fill"),
Source = source
};
rect.SetBinding(Shape.FillProperty, binding);
最后,您只需启动BackgroundAnimation。
我想你仍然可以使用'ObjectAnimationUsingKeyFrames'为'Brushes'制作动画? –