none
请问在wpf的动画中,怎样让一个小球按照指定的路线移动 RRS feed

全部回复

  • 你好,

    这个应该是路径动画实现的效果,我给你一个简单的例子参照:

     <Window.Resources>
            <PathGeometry x:Key="path">
                <PathFigure IsClosed="True">
                    <ArcSegment Point="100,200" Size="15,10" SweepDirection="Clockwise"></ArcSegment>
                    <ArcSegment Point="400,50" Size="5,5" ></ArcSegment>
                </PathFigure>
            </PathGeometry>
        </Window.Resources>
        <Window.Triggers>
            <EventTrigger RoutedEvent="Window.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimationUsingPath Storyboard.TargetName="ellipse"
                                          Storyboard.TargetProperty="(Canvas.Left)"
                                            PathGeometry="{StaticResource path}"
                                          Duration="0:0:5" RepeatBehavior="Forever" Source="X" />
                            <DoubleAnimationUsingPath Storyboard.TargetName="ellipse"
                                          Storyboard.TargetProperty="(Canvas.Top)"
                                         PathGeometry="{StaticResource path}"
                                          Duration="0:0:5" RepeatBehavior="Forever" Source="Y" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Window.Triggers>
        <Grid>
            <Canvas Margin="10">
                <Path Stroke="Red" StrokeThickness="1" Data="{StaticResource path}" Canvas.Top="10" Canvas.Left="10">
                </Path>
                <Ellipse Name="ellipse" Fill="Black" Width="10" Height="10"></Ellipse>
            </Canvas>
        </Grid>

    效果图如下:

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2021年2月1日 1:55