10

我试图让多边形完全从屏幕左侧移动,穿过屏幕,然后完全从屏幕右侧移动,然后再返回。

我已经得到了这个工作。但是,由于某种原因,一旦左边距变为负数,动画就会突然变慢。一旦左边距变为正数,它就会再次加速。

为什么会这样?我怎样才能阻止它?

这是演示这一点的完整代码:

<Window x:Class="Geometry.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <PathGeometry x:Key="MyGeometry">
            <PathGeometry.Figures>
                <PathFigure>
                    <PathFigure.Segments>
                        <LineSegment Point="0.30,0" />
                        <LineSegment Point="0.70,1" />
                        <LineSegment Point="0.40,1" />
                        <LineSegment Point="0,0" />
                    </PathFigure.Segments>
                </PathFigure>
            </PathGeometry.Figures>
        </PathGeometry>
        <Storyboard x:Key="MovingAnimation">
            <ThicknessAnimationUsingKeyFrames RepeatBehavior="1:0:0" FillBehavior="HoldEnd" Storyboard.TargetName="_path" Storyboard.TargetProperty="Margin" >
                <DiscreteThicknessKeyFrame KeyTime="0:0:0" Value="-2.0,0,0,0"  />
                <LinearThicknessKeyFrame KeyTime="0:0:10" Value="1.0,0,0,0"  />
                <LinearThicknessKeyFrame KeyTime="0:0:20" Value="-2.0,0,0,0"  />
            </ThicknessAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard Storyboard="{StaticResource MovingAnimation}" ></BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <Label>Margin:</Label>
            <TextBlock Text="{Binding ElementName=_path, Path=Margin.Left, StringFormat={}{0:0.#}}" />
        </StackPanel>
    <Canvas Name="_canvas" Grid.Row="1">
        <Border Margin="0" Width="1" Height="1" VerticalAlignment="Center" HorizontalAlignment="Center">
            <Border.RenderTransform>
                <ScaleTransform 
                    ScaleX="{Binding ElementName=_canvas, Path=ActualWidth}" 
                    ScaleY="{Binding ElementName=_canvas, Path=ActualHeight}"
                    CenterX="0"
                    CenterY="0">
                </ScaleTransform>
            </Border.RenderTransform>
            <Path 
                Name="_path" 
                Fill="#CCCCFF" 
                Data="{StaticResource MyGeometry}" 
                Width="1.0" 
                Height="1.0" 
                >
        </Path>
        </Border>
    </Canvas>
    </Grid>
</Window>
4

3 回答 3

4

我没有解释为什么负边距动画缓慢,但我找到了一种解决方法。

我没有为包含路径的对象上的 a 值设置动画Margin,而不是动画。我必须把它放在前面,以便在比例之前应用翻译。这允许您在动画中使用与用于.PathXTranslateTransformBorderTranslateTransformScaleTransformMargin

    <Storyboard x:Key="MovingAnimation">
        <ThicknessAnimationUsingKeyFrames RepeatBehavior="1:0:0"  Storyboard.TargetName="_blank" Storyboard.TargetProperty="Margin" >
            <LinearThicknessKeyFrame KeyTime="0:0:0" Value="-1.5,0,0,0"  />
            <LinearThicknessKeyFrame KeyTime="0:0:10" Value="1,0,0,0"  />
            <LinearThicknessKeyFrame KeyTime="0:0:20" Value="-1.5,0,0,0"  />
        </ThicknessAnimationUsingKeyFrames>
    </Storyboard>

丑陋的部分是我找不到将关键帧中的值直接应用于 的X属性的快速方法TranslateTransform,所以我作弊并使用了元素绑定和占位符Canvas对象。

    <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5">
        <TextBlock Margin="0,0,5,0" Text="Margin.Left:"/>
        <TextBlock Text="{Binding ElementName=_blank, Path=Margin.Left, StringFormat={}{0:0.#}}" />
    </StackPanel>
    <Canvas Name="_blank" />  <!--Placeholder object-->
    <Canvas Name="_canvas" Grid.Row="1">
        <Border Margin="0" Width="1" Height="1"
                Name="_border"
                VerticalAlignment="Center" HorizontalAlignment="Center">
            <Border.RenderTransform>
                <TransformGroup>
                    <TranslateTransform X="{Binding Margin.Left, ElementName=_blank}"/>
                    <ScaleTransform 
                        ScaleX="{Binding ElementName=_canvas, Path=ActualWidth}" 
                        ScaleY="{Binding ElementName=_canvas, Path=ActualHeight}"
                        CenterX="0"
                        CenterY="0">
                    </ScaleTransform>
                </TransformGroup>
            </Border.RenderTransform>

即使动画仍然被应用到 aMargin然后再应用到TranslateTransform通过元素绑定,负边距的延迟已经消失了。

我怀疑负边距延迟与PathBorder缩放的存在有关,但这是我的猜测。

如果您能找到一种方法将KeyFrame值直接绑定到 的X属性TranslateTransform,那将使这种解决方法变得不那么难看。

编辑: 找出要使用的正确绑定:

    <Storyboard x:Key="MovingAnimation2">
        <DoubleAnimationUsingKeyFrames RepeatBehavior="1:0:0"  Storyboard.TargetName="tt" Storyboard.TargetProperty="X" >
            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="-1.5"  />
            <LinearDoubleKeyFrame KeyTime="0:0:5" Value="1"  />
            <LinearDoubleKeyFrame KeyTime="0:0:10" Value="-1.5"  />
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

这摆脱了额外的画布占位符对象。

    <Canvas Name="_canvas" Grid.Row="1">
        <Border Margin="0" Width="1" Height="1"
                Name="_border"
                VerticalAlignment="Center" HorizontalAlignment="Center">
            <Border.RenderTransform>
                <TransformGroup>
                    <TranslateTransform x:Name="tt"/>
                    <ScaleTransform 
                        ScaleX="{Binding ElementName=_canvas, Path=ActualWidth}" 
                        ScaleY="{Binding ElementName=_canvas, Path=ActualHeight}"
                        CenterX="0"
                        CenterY="0">
                    </ScaleTransform>
                </TransformGroup>
            </Border.RenderTransform>
于 2013-06-10T14:33:10.263 回答
3

动画边距属性将触发额外的测量/排列通道,这反过来会导致更多的性能影响(尽管在此示例中可能不明显)。另一方面,“仅渲染”属性的动画不会触发布局重新排列,因此对性能更加友好。

请看一下更简单的方法来做我想你想要得到的东西:

<Window x:Class="Geometry.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="518" Width="530">
<Window.Resources>
    <PathGeometry x:Key="MyGeometry">
        <PathGeometry.Figures>
            <PathFigure>
                <PathFigure.Segments>
                    <LineSegment Point="0.30,0" />
                    <LineSegment Point="0.70,1" />
                    <LineSegment Point="0.40,1" />
                    <LineSegment Point="0,0" />
                </PathFigure.Segments>
            </PathFigure>
        </PathGeometry.Figures>
    </PathGeometry>
    <Storyboard x:Key="MovingAnimation">
        <DoubleAnimationUsingKeyFrames RepeatBehavior="1:0:0" FillBehavior="HoldEnd" Storyboard.TargetName="_scaleTransform" Storyboard.TargetProperty="CenterX" >
            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="1.2" />
            <LinearDoubleKeyFrame KeyTime="0:0:10" Value="-0.5" />
            <LinearDoubleKeyFrame KeyTime="0:0:20" Value="1.2" />
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>
<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <BeginStoryboard Storyboard="{StaticResource MovingAnimation}" ></BeginStoryboard>
    </EventTrigger>
</Window.Triggers>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Orientation="Horizontal">
        <Label>Margin:</Label>
        <TextBlock Text="{Binding ElementName=_scaleTransform, Path=CenterX, StringFormat={}{0:0.#}}" VerticalAlignment="Center" />
    </StackPanel>
<!--
    <Border Grid.Row="1" Margin="150" BorderBrush="Red" BorderThickness="1">
-->
    <Grid Name="_canvas" Grid.Row="1">
        <Path Name="_path" Fill="#CCCCFF" Data="{StaticResource MyGeometry}" 
              Width="1.0"
              Height="1.0"
              HorizontalAlignment="Center"
              VerticalAlignment="Center">
            <Path.RenderTransform>
                <ScaleTransform x:Name="_scaleTransform"
                    ScaleX="{Binding ElementName=_canvas, Path=ActualWidth}" 
                    ScaleY="{Binding ElementName=_canvas, Path=ActualHeight}"
                    CenterX="1.2"
                    CenterY="0.5">
                </ScaleTransform>
            </Path.RenderTransform>
        </Path>
    </Grid>
<!--
    </Border>
-->
</Grid>
</Window>
于 2013-06-10T14:33:57.217 回答
1

从到目前为止的两个答案中,很明显,如果您想让一个形状在屏幕上飞来飞去,请不要为边缘设置动画。

  • Stewbob 通过为平移变换的 X 值设置动画解决了这个问题。
  • Sevenate 通过对比例变换的中心 X 值进行动画处理来解决这个问题。

另一种解决方案是,不是将多边形包裹在边框中并为边距设置动画,而是将其包裹在画布中并对左值进行动画处理。

将其包装在画布中:

    <Canvas Name="_canvasFrame" Grid.Row="1">
        <Canvas Margin="0" Width="1" Height="1">
            <Canvas.RenderTransform>
                <ScaleTransform 
                ScaleX="{Binding ElementName=_canvasFrame, Path=ActualWidth}" 
                ScaleY="{Binding ElementName=_canvasFrame, Path=ActualHeight}"
                CenterX="0"
                CenterY="0">
                </ScaleTransform>
            </Canvas.RenderTransform>
            <Path 
            Name="_path" 
            Fill="#CCCCFF" 
            Data="{StaticResource MyGeometry}" 
            Width="1.0" Height="1.0" 
            >
            </Path>
        </Canvas>
    </Canvas>

然后动画左值:

    <Storyboard x:Key="MovingAnimation">
        <DoubleAnimationUsingKeyFrames 
                          RepeatBehavior="1:0:0" 
                          FillBehavior="HoldEnd" 
                          Storyboard.TargetName="_path" 
                          Storyboard.TargetProperty="(Canvas.Left)" >
            <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="-1.0"  />
            <LinearDoubleKeyFrame KeyTime="0:0:10" Value="1.0"  />
            <LinearDoubleKeyFrame KeyTime="0:0:20" Value="-1.0"  />
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
于 2013-06-10T22:24:24.720 回答