使用 WPF 实现平滑文本动画(选取框)

发布于 2024-09-05 08:58:02 字数 1593 浏览 10 评论 0原文

尝试构建具有流畅文本动画的选取框控件。当前的工作包括:

  • 使用平移变换
  • 在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2024-09-12 08:58:02

如果满足以下条件,您的动画将完全在 MilCore 层处理:

  1. 您的 TranslateTransform 是 RenderTransform(不是 LayoutTransform),并且
  2. 您使用简单的动画,例如 DoubleAnimation,并且
  3. 您的对象没有剪切或不透明度计算

尝试使用 DoubleAnimation 动画 TranslateTransform对于 TextBlock 上的 RenderTransform,该 TextBlock 是具有默认设置的 Window 的直接子级。

  • 如果仍然很慢,则说明您的 Direct3D 系统有些慢,因为根本不涉及托管代码,而且 MilCore 的调用非常简单,但是

  • 如果它运行顺利且高效,请逐步将其更改为性能不佳的代码,看看是什么更改导致速度变慢。

鉴于您对 Jobi Joy 的回答的回应,我怀疑问题出在您的硬件或 Direct3D 设置中,但找出答案的唯一方法是测试它。

Your animation will be handled entirely at the MilCore layer if:

  1. Your TranslateTransform is a RenderTransform (not a LayoutTransform), and
  2. You use a simple animation such as a DoubleAnimation, and
  3. Your object has no clipping or opacity calculations

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.

诗笺 2024-09-12 08:58:02

如果您使用的是 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 a TextBlock.

雪花飘飘的天空 2024-09-12 08:58:02

希望这可以帮助您 - 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文