简体   繁体   English

如何在WPF中将动画缩放到窗口大小

[英]How can I scale an animation in wpf to the size of the window

I want an animation that adapts to the size of the window. 我想要一个适合窗口大小的动画。

I copied the code from the pointanimationusingpath example and changed it a little. 我从pointanimationusingpath 示例中复制了代码,并对其进行了一些更改。 I added a new path so I could see the path the circle follows. 我添加了一条新路径,以便可以看到圆所遵循的路径。 I also made a simpler path for the circle to follow. 我还为圈子遵循了一条简单的道路。

I can add Stretch="Fill" to the path that shows the path of the circle. 我可以将Stretch="Fill"添加到显示圆的路径的路径。 Now the size of the path changes with the window size. 现在,路径的大小随窗口大小而变化。 How do I do this with the path of the animation and make it follow the same path as the path shown? 我该如何使用动画的路径并使其遵循与所示路径相同的路径?

<Window x:Class="TestPointAlongPath.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
        mc:Ignorable="PresentationOptions"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Path Data="M1,200 L275,1 L345,200" Margin="15,15,15,15" 
                        Stroke="#BF708090" StrokeThickness="3" />

        <Path Fill="Blue" Margin="15,15,15,15" >
            <Path.Data>

                <!-- The EllipseGemetry specifies the shape and position of the Ellipse. The
            Center property is animated, causing the Ellipse to animate across the screen-->
                <EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
                Center="10,100" RadiusX="15" RadiusY="15"/>
            </Path.Data>
            <Path.Triggers>
                <EventTrigger RoutedEvent="Path.Loaded">
                    <BeginStoryboard Name="MyBeginStoryboard">
                        <Storyboard>

                            <!-- Animates the ellipse along the path. -->
                            <PointAnimationUsingPath
                    Storyboard.TargetName="MyAnimatedEllipseGeometry"
                    Storyboard.TargetProperty="Center"
                    Duration="0:0:7.5" 
                    RepeatBehavior="Forever" >
                                <PointAnimationUsingPath.PathGeometry>
                                    <PathGeometry 
                        Figures="M1,200 L275,1 L345,200"
                        PresentationOptions:Freeze="True">
                                        <PathGeometry.Transform>
                                            <ScaleTransform ScaleX="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=Grid}}"
                                                            ScaleY="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType=Grid}}"/>
                                        </PathGeometry.Transform>
                                    </PathGeometry>
                                </PointAnimationUsingPath.PathGeometry>
                            </PointAnimationUsingPath>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Path.Triggers>
        </Path>
    </Grid>
</Window>

You hosted the Paths under Canvas. 您将“路径”托管在“画布”下。 Canvas demands exact locations. 画布要求确切的位置。 Instead use Grid. 而是使用网格。 All Paths will sit one on top of the other. 所有路径都将一个放在另一个之上。 Then, you can use their Stretch property to work out their stretching. 然后,您可以使用其Stretch属性来进行拉伸。 Also, you dont need the Page element.. 另外,您不需要Page元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM