【发布时间】:2017-12-04 15:05:44
【问题描述】:
我知道关于水平文本动画/文本滚动有很多不同的主题,但不幸的是,它们都没有提供带有可重复文本的平滑滚动。我尝试过使用包含文本的各种 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 marquee 的可重复文本,没有任何间隙,可能需要将其扩展到多个显示器。
请提出建议。
【问题讨论】:
标签: c# wpf directx sharpdx wpf-animation