使用 WPF 实现平滑文本动画(选取框)
尝试构建具有流畅文本动画的选取框控件。当前的工作包括:
- 使用平移变换
- 在 Canvas 依赖属性上使用动画(左、右)
- 在自定义依赖属性(点)上使用动画并使用绘图视觉效果(格式化文本)
- 使用 CompositionTarget.Rendering
但动画仍然不稳定且资源密集(2- 10% CPU)。
默认 wpf 窗口中使用的测试代码,我认为应该产生平滑的动画:
<TextBlock x:Name="_box" FontSize="64" CacheMode="BitmapCache" Text="lorem ipsum">
<TextBlock.RenderTransform>
<TranslateTransform x:Name="AnimatedTranslateTransform" X="0" Y="0" />
</TextBlock.RenderTransform>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="AnimatedTranslateTransform"
Storyboard.TargetProperty="X"
From="-300" To="300" Duration="0:0:5"
AutoReverse="True" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
检查表:
- 确认没有发生软件渲染(ms 性能工具并检查 RenderCapability.Tier)
- 在任何可想象的对象上调用冻结
- 禁用任何位图效果和透明度
- 检查所有选取框控制在那里(同样的问题)
测试:
- CPU:Intel core 2 duo(T6600)@2.2Ghz
- RAM:4GB
- GPU:NVidia GeForce 9600M GS(最新驱动程序)
- 操作系统:Windows 7(64位)
任何想法(或更好的代码示例) )?
从响应来看,这似乎不是 wpf 问题(其他选框控件对其他人来说效果很好,但对我来说则不然),但我在测试的每台机器上都遇到了相同的问题。
Trying to build a marquee control with smooth text animation. Current efforts include:
- Using translate transform
- Using animation on Canvas dependency properties (Left, Right)
- Using animation on custom dependency property (Point) and using drawing visuals (formattedtext)
- Using CompositionTarget.Rendering
But the animation is still choppy and resource intensive (2-10% CPU).
Test code used in default wpf window which I assume should produce a smooth animation:
<TextBlock x:Name="_box" FontSize="64" CacheMode="BitmapCache" Text="lorem ipsum">
<TextBlock.RenderTransform>
<TranslateTransform x:Name="AnimatedTranslateTransform" X="0" Y="0" />
</TextBlock.RenderTransform>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="AnimatedTranslateTransform"
Storyboard.TargetProperty="X"
From="-300" To="300" Duration="0:0:5"
AutoReverse="True" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
Checklist:
- Confirmed no software rendering is taking place (ms performance tool and checking RenderCapability.Tier)
- Calling freeze on any imaginable object
- Disabled any bitmap effect and transparency
- Checked all marquee controls out there (same issues)
Tested on:
- CPU: Intell core 2 duo (T6600) @2.2Ghz
- RAM: 4GB
- GPU: NVidia GeForce 9600M GS (latest drivers)
- OS: Windows 7 (64bit)
Any ideas (or better yet code example)?
From the responses it seems this is not a wpf issue (other marquee controls work fine for others but not for me), nut I'm getting the same issues on every machine I tested this on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果满足以下条件,您的动画将完全在 MilCore 层处理:
尝试使用 DoubleAnimation 动画 TranslateTransform对于 TextBlock 上的 RenderTransform,该 TextBlock 是具有默认设置的 Window 的直接子级。
如果仍然很慢,则说明您的 Direct3D 系统有些慢,因为根本不涉及托管代码,而且 MilCore 的调用非常简单,但是
如果它运行顺利且高效,请逐步将其更改为性能不佳的代码,看看是什么更改导致速度变慢。
鉴于您对 Jobi Joy 的回答的回应,我怀疑问题出在您的硬件或 Direct3D 设置中,但找出答案的唯一方法是测试它。
Your animation will be handled entirely at the MilCore layer if:
Try using a DoubleAnimation-animated TranslateTransform for a RenderTransform on a TextBlock that is a direct child of a Window with default settings.
If this is still slow, there is something slow about your Direct3D system because managed code is not involved at all and MilCore's calls are very simple, but
If it works smoothly and efficiently, incrementally change it to your poorly-performing code to see what change causes the slowdown.
Given your response to Jobi Joy's answer I would suspect the problem is somewhere in your hardware or Direct3D setup, but the only way to find out is to test it.
如果您使用的是 WPF 4.0,请尝试在要设置动画的元素上设置
CacheMode="BitmapCache"
(在 XAML 中),在本例中,可能是TextBlock
。If you are using WPF 4.0, try setting the
CacheMode="BitmapCache"
(in the XAML) on the element you are animating, in this case, probably aTextBlock
.希望这可以帮助您 - http://jobijoy.blogspot.com/ 2008/08/silverlight-marquee-control.html
WPF版本也可以找到此处
Hope this may help you - http://jobijoy.blogspot.com/2008/08/silverlight-marquee-control.html
And the WPF version also can be found here