繁体   English   中英

如何创建平滑的动画文字字幕?

[英]How to create a smooth animated text marquee?

我知道关于水平文本动画/文本滚动有很多不同的主题,但是不幸的是,它们都没有提供可重复文本的平滑滚动。 我已经尝试使用各种包含文本的WPF控件来制作双倍/厚度动画。 还尝试了使用视觉画笔进行动画设置的效果,相比其他方法(例如,使用Canvas.Left属性等),它给了我迄今为止最优雅的滚动效果,但是如果文本长度或动画速度太高,也会模糊文本。

我要结束使用SharpDX库的DirectX C#实现。 还应该提到我是DirectX编程的初学者。 这是代码:

public void RunMethod()
{
    // Make window active and hide mouse cursor.
    window.PointerCursor = null;
    window.Activate();

    var str = "This is an example of a moving TextLayout object with no snapped pixel boundaries.";

    // Infinite loop to prevent the application from exiting.
    while (true)
    {
        // Dispatch all pending events in the queue.
        window.Dispatcher.ProcessEvents(CoreProcessEventsOption.ProcessAllIfPresent);

        // Quit if the users presses Escape key.
        if (window.GetAsyncKeyState(VirtualKey.Escape) == CoreVirtualKeyStates.Down)
        {
            return;
        }

        // Set the Direct2D drawing target.
        d2dContext.Target = d2dTarget;

        // Clear the target. 
        d2dContext.BeginDraw();
        d2dContext.Clear(Color.CornflowerBlue);

        //float layoutXOffset = 0;
        float layoutXOffset = layoutX;

        // Create the DirectWrite factory objet.
        SharpDX.DirectWrite.Factory fontFactory = new SharpDX.DirectWrite.Factory();

        // Create a TextFormat object that will use the Segoe UI font with a size of 24 DIPs.
        textFormat = new TextFormat(fontFactory, "Verdana", 100.0f);

        textLayout2 = new TextLayout(fontFactory, str, textFormat, 2000.0f, 100.0f);

        // Draw moving text without pixel snapping, thus giving a smoother movement.
        // d2dContext.FillRectangle(new RectangleF(layoutXOffset, 1000, 1000, 100), backgroundBrush);
        d2dContext.DrawTextLayout(new Vector2(layoutXOffset, 0), textLayout2, textBrush, DrawTextOptions.NoSnap);

        d2dContext.EndDraw();

        //var character = str.Substring(0, 1);
        //str = str.Remove(0, 1);
        //str += character;
        layoutX -= 3.0f;

        if (layoutX <= -1000)
        {
            layoutX = 0;
        }

        // Present the current buffer to the screen.
        swapChain.Present(1, PresentFlags.None);
    }
}

基本上,它创建了一个无限循环并减去了水平偏移。 这里是挑战:我需要类似于HTML字幕的可重复文本,且没有任何间隙,可能需要将其扩展到多个监视器。

请提出建议。

我既不知道如何使用DirectX也不知道Sharpdx,但是如果您愿意,可以考虑使用此解决方案

前段时间我也遇到了类似的问题,但是组合框内的文本。 赏金后,我得到了我想要的东西。 我以相关代码段为例,但是您可以在此处查看完整的答案

基本上,只要您的文本块/文本框包含无法完全显示的字符串,并且导致长度超过文本块/框长度,就可以使用这种方法。 您可以定义一个从所需基础派生的自定义用户控件(例如SlidingComboBox:Combobox),并为您的故事板定义一个动画,如下所示

_animation = new DoubleAnimation()
 {
      From = 0,
      RepeatBehavior = SlideForever ? RepeatBehavior.Forever : new RepeatBehavior(1), //repeat only if slide-forever is true
      AutoReverse = SlideForever
 };

在我的示例中,我希望仅当鼠标在组合框上时才激活此行为,因此在我的自定义OnMouse中输入以下代码

if (_parent.ActualWidth < textBlock.ActualWidth)
{
     _animation.Duration = TimeSpan.FromMilliseconds(((int)textBlock.Text?.Length * 100));
     _animation.To = _parent.ActualWidth - textBlock.ActualWidth;
     _storyBoard.Begin(textBlock);
}

其中_parent代表所选项目的容器。 在检查了文本长度与组合框长度之后,我开始动画并在要显示的文本结尾处结束动画

请注意,在我提到的问题中,还有其他解决方案。 我正在发布对我有用的那个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM