【问题标题】:A smooth transition from the old to the new values in WPFWPF 中从旧值到新值的平滑过渡
【发布时间】:2012-09-08 05:18:25
【问题描述】:

在窗口中是一个有一定大小的矩形,我需要为矩形设置一个新的大小,逐渐改变它的旧大小到新的大小。或者,例如,必须转动平滑的矩形。怎么做?

编辑

我想我表达得不好。 例如,我有一个尺寸为 200 x 300 的矩形。我正在尝试使用新尺寸:400 x 200,我希望他没有快速应用新的平滑动画命中目标值。

我想这在 WPF 中是如何完成的?

【问题讨论】:

    标签: wpf properties transform transition smooth


    【解决方案1】:

    不知道你说的把那个平滑的矩形转过来是什么意思。

    故事板。该示例甚至显示了调整矩形的大小

    Storyboards Overview

      <StackPanel Margin="20">
                <Rectangle Name="MyRectangle"
          Width="100"
          Height="100">
                    <Rectangle.Fill>
                        <SolidColorBrush x:Name="MySolidColorBrush" Color="Blue" />
                    </Rectangle.Fill>
                    <Rectangle.Triggers>
                        <EventTrigger RoutedEvent="Rectangle.MouseEnter">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation 
                    Storyboard.TargetName="MyRectangle"
                    Storyboard.TargetProperty="Width"
                    From="100" To="200" Duration="0:0:1" />
    
                                    <ColorAnimation 
                    Storyboard.TargetName="MySolidColorBrush"
                    Storyboard.TargetProperty="Color"
                    From="Blue" To="Red" Duration="0:0:1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Rectangle.Triggers>
                </Rectangle>
            </StackPanel>
    

    【讨论】:

    • 这正是链接中示例的作用。你试过了吗?
    • 我不知道我该如何使用它。我为此更改了设置: retangle1.Size = new Size(400,200);我需要对动画应用新设置。
    • 所以你没有尝试,因为这正是故事板和双动画所做的。
    • 这不是新的尺寸总是任意的,sledovatelnoi 旧的任意。 private void Button_Click(object sender, RoutedEventArgs e) { MyRectangle.Height = 400; MyRectangle.Width = 300; }
    【解决方案2】:

    老问题,但我有同样的问题并找到了解决方案。

    我认为用 xaml 是不可能的,只能在代码中。

    using System.Windows.Media.Animation; // this is needed for use DoubleAnimation in code
    
    ...
    
    private void ResizeRectangle(double width, double height)
    {
        // Height
        DoubleAnimation a = new DoubleAnimation();
        a.From = MyRectangle.ActualHeight; // animation start with actual height
        a.To = height; // desired height
        a.Duration = new Duration(TimeSpan.FromSeconds(1)); // duration for From to To (you can use miliseconds), this make smooth transition from the old to the new values
        a.FillBehavior = FillBehavior.Stop; // FillBehavior should be stop, if you want do another size changes
        MyRectangle.BeginAnimation(Rectangle.HeightProperty, a);  // set up animation for MyRectangle
    
        MyRectangle.Height = height; // don't forget change size for rectangle, because FillBehavior.Stop stop animation and it set back to actual size
    
        // Width
        DoubleAnimation b = new DoubleAnimation();
        b.From = MyRectangle.ActualWidth;
        b.To = width;
        b.Duration = new Duration(TimeSpan.FromSeconds(1));
        b.FillBehavior = FillBehavior.Stop;
        MyRectangle.BeginAnimation(Rectangle.WidthProperty, b);
    
        MyRectangle.Width = width;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-07
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 2018-07-15
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多