您的垂直跳跃问题是由于字体渲染舍入造成的。具体来说,WPF 将避免亚像素字体高度,以实现字体平滑。避免这种情况的一种方法是将文本转换为路径几何图形,然后使用比例变换对其进行动画处理。
这是示例的替代版本,没有跳转。新的 XAML 是:
<Grid>
<Path Name="Path" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
以及加载窗口时的新代码:
SetText("");
var transform = new ScaleTransform(1, 1);
Path.LayoutTransform = transform;
var animationX = new DoubleAnimation(1, 10, new Duration(TimeSpan.FromSeconds(60)));
transform.BeginAnimation(ScaleTransform.ScaleXProperty, animationX);
var animationY = new DoubleAnimation(1, 10, new Duration(TimeSpan.FromSeconds(60)));
transform.BeginAnimation(ScaleTransform.ScaleYProperty, animationY);
以及设置动画文本的新方法:
private void SetText(string text)
{
var formatted = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Lucida Console"), 12, Brushes.Black);
Path.Data = formatted.BuildGeometry(new Point(0, 0));
Path.Fill = Brushes.Black;
}
并且您已从计时器事件处理程序中调用 SetText。
请注意,为了避免水平跳跃,您必须使用固定长度的文本字符串和恒定宽度的字体。