0

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

编辑

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

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

4

2 回答 2

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;
}
于 2019-07-02T18:01:40.213 回答
0

不知道转动平滑的矩形是什么意思。

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

故事板概述

  <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>
于 2012-09-15T14:01:46.987 回答