优化淡入淡出框动画
我正在尝试为我的 WPF 应用程序构建交互式背景。正如您从下面的屏幕截图中可以看出的,它由分散在背景中的各个矩形组成,我希望它们单独淡入和淡出。
我正在根据用户计算机的虚拟屏幕尺寸计算所需的矩形数量(例如,我的是 3200x1200)。这样,最大化和最小化窗口将显示更多背景。如前所述,对于我的分辨率,我需要 3220 个矩形。
我现在实现的方式是将所有矩形添加到具有随机生成的 alpha 值的画布中。然后我延迟开始自动循环动画(见下文)。不幸的是,这导致我的应用程序极其缓慢(理所当然)。无论如何,我可以实现这种类型的效果并获得更好的性能吗?
<Storyboard x:Key="uiStoryboardTile" AutoReverse="True" RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0:0:2" Value="Transparent"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
I am trying to build an interactive background for my WPF application. As you can tell from the screenshot below, it consists of individual rectangles scattered throughout the background and I want them to individually fade in and out.
I am calculating the amount of rectangles required based on the Virtual Screen Size of the users machine (mine is 3200x1200 for example). That way maximizing and minimizing the window will reveal more of the background. As stated, for my resolution I am going to need 3220 rectangles.
The way I have this implemented now is all rectangles are added to a canvas with randomly generated alpha values. I then delay-begin an auto-looping animation (see below). Unofrtunatly this is causing my application to be extremely sluggish (rightfully so). Is there anyway I can implement this type of effect and have much better performance?
<Storyboard x:Key="uiStoryboardTile" AutoReverse="True" RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0:0:2" Value="Transparent"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是我将采取的措施来提高性能:
1)仅创建在背景中可见的矩形。
2) 将每个 Rectangle 的 Fill 属性随机绑定到多个预定义颜色的 StaticResources 之一(10-20 应该可以解决问题)。
3) 创建一个故事板来对这些静态资源进行动画处理。
您将失去每个矩形的个性,但它应该在不终止您的应用程序的情况下进行动画处理。
编辑 - 代码示例
例如,添加一些资源进行动画处理(它们是矩形,因为您无法直接对 SolidColorBrush 进行动画处理):
您的故事板将类似于:
在您生成的代码隐藏中所有矩形,您都需要绑定到资源。因此,在循环中的某个位置,您需要添加:
最后,您所要做的就是启动BackgroundAnimation。
Here's what I would do to improve performance:
1) Create only the Rectangles that are visible in the background.
2) Randomly bind the Fill property of each Rectangle to one of a number of StaticResources of predefined colors (10-20 should probably do the trick).
3) Create a storyboard that animates those StaticResources.
You would lose the individualality each Rectangle, but it should animate without killing your application.
EDIT - Code examples
For example, add some resources to animate (they're rectangles since you can't directly animate a SolidColorBrush):
Your Storyboard would look something like:
In your code-behind where you generate all the rectangles, you'll need to bind to the resources. So somewhere in the loop, you'll need to add:
Finally, all you'll have to do is start the BackgroundAnimation.
有几个选项
:不要使用那么多矩形。我知道这是显而易见的,但说真的,即使对于高性能机器来说,要制作三千个动画项目也很多。
b.尝试对不同矩形的不透明度而不是填充画笔的颜色进行动画处理。根据我的疯狂猜测(WAG)理论,这可能会产生积极的影响。
c.尝试手动设置每个矩形的不透明度/颜色的动画,而不是使用故事板来调整颜色。例如:
您会注意到一个特殊的类,其中包含一些满足您需求的额外信息。
当然,您还需要添加一些支持代码,例如将矩形连接到其主机等,但听起来您已经自己做到了这一点。
There are a couple of options:
a. Don't use so many rectangles. I know this is obvious, but seriously, three thousand items to animate is A LOT even for a performant machine.
b. Try animating the opacity of the different rectangles instead of the color on the fill brush. According to my wild ass guess (WAG) theory, this may have a positive effect.
c. Try animating the opacities / colors of each of the rectangles manually instead of using storyboards to tweak the color. For example:
You will notice a special class that has a bit of extra information for your needs.
Of course there is still some more support code that you will have to put in place, like getting your rectangles to their host, etc. but it sounds like you have already gotten that far on your own.
您是否尝试过将它们分组制作动画?虽然您可能有 1000 多个矩形,但也许您可以将它们分成几组,并且一次只运行 10 个左右的动画。如果块的间距正确,它仍应具有随机动画矩形的外观。
这可以通过创建块层来完成,其中每个层都是透明的,只有它的块在其上可见。将图层堆叠在一起并为每个图层设置 Alpha 动画。
Have you tried animating them in groups? While you may have 1000+ rectangles perhaps you could break them up in to groups and only have 10 or so animations running at once. If the blocks are spaced correctly it should still have the appearance of randomly animated rectangles.
This could be done by creating layers of blocks, where each layer is transparent with just it's blocks visible on it. Stack the layers on top of each other and animate the alpha of each layer.