WPF动画很糟糕

问题描述 投票:0回答:1

我正在尝试使用一些动画来让我的应用程序感觉良好。但我无法帮助那些不稳定的动画,无论我做什么,它总是最终结结巴巴。

看一看:

DoubleAnimation anim = new DoubleAnimation()
                 {
//ht is height of DockPanel, I wanted to start from 200 less than Actual DockPanel Height
                         From = ht - 200,
                         To = ht,
                         Duration = TimeSpan.FromSeconds(1),
                         AccelerationRatio = 0.5,
                         DecelerationRatio = 0.5
                     };
    x.BeginAnimation(HeightProperty, anim);
    //x is the UserControl

此外,我需要动画的是一个自定义UserControl,其中包含一些文本,如100个单词和一堆图像。我只是想在加载后立即将其扩展到当前DockPanel的高度。

我通过搜索解决方案看到的是这个,

Timeline.SetDesiredFrameRate(anim, 10);

即使在那里尝试任何价值也没有真正发生。

c# .net wpf animation wpf-controls
1个回答
0
投票

帧率就像电影的帧速率。

较低的帧速率会给你一个不连贯的电影或不稳定的动画。

对于您要制作动画的某些内容,使用dockpanel可能是一个坏主意,因为它会在每次高度变化时尝试调整内容。

我建议你换一个网格。

您应该使用scaletransform。部分原因是,当您设置动画高度时,您会发现用户控件的所有内容都使其度量无效,并且他们希望多次开始整个度量安排周期。

如果您正在考虑安排措施?然后阅读wpf布局系统的工作原理。

我还建议你使用xaml而不是代码。

以下是一些需要思考的代码:

    private void StartAnimate_Click(object sender, RoutedEventArgs e)
    {
        var tran = testRectangle.RenderTransform = new ScaleTransform(1d, 1d)
        {
            CenterX = 0.5d,
            CenterY = 0.5d
        };
        var anim = new DoubleAnimation
        {
            To = 1.0,
            From=0,
            Duration = TimeSpan.FromSeconds(0.5d),
            DecelerationRatio = 0.5d,
            FillBehavior = FillBehavior.Stop
        };
        tran.BeginAnimation(
             ScaleTransform.ScaleYProperty,
             anim,
             HandoffBehavior.Compose);
    }

我在一个dockpanel中放了一个矩形和我的按钮来证明这是有效的。

<DockPanel>
    <Rectangle Fill="Blue" Name="testRectangle"
               Width="500"
    />
    <Button Content="Animate"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Name="StartAnimate"
            Click="StartAnimate_Click"  />
</DockPanel>

矩形非常简单,但动画效果很流畅。

© www.soinside.com 2019 - 2024. All rights reserved.