WPF关键帧动画2

本文展示了如何在WPF中使用XAML创建关键帧动画,通过一个PathGeometry定义路径,使图像沿着该路径进行平移动画。具体代码包括设定Window.Resources中的PathGeometry,设置Window.Triggers中的EventTrigger来触发动画,并定义DoubleAnimationUsingPath来控制图像在Canvas上的左右(X轴)和上下的(Y轴)移动。动画效果为图像沿着红线条路径无限循环移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XAML代码:

<Window.Resources>

        <PathGeometry x:Key="path">

            <PathFigure IsClosed="True">

                <ArcSegment Point="100,200" Size="15,10" SweepDirection="Clockwise"></ArcSegment>

                <ArcSegment Point="400,5" Size="5,5"></ArcSegment>

            </PathFigure>

        </PathGeometry>

    </Window.Resources>

    <Window.Triggers>

        <EventTrigger RoutedEvent="Window.Loaded">

            <EventTrigger.Actions>

                <BeginStoryboard>

                    <Storyboard>

                        <DoubleAnimationUsingPath Storyboard.TargetName="image" Storyboard.TargetProperty="(Canvas.Left)" PathGeometry="{StaticResource path}" Duration="0:0:5" RepeatBehavior="Forever" Source ="X"/>

                        <DoubleAnimationUsingPath Storyboard.TargetName="image" Storyboard.TargetProperty="(Canvas.Top)" PathGeometry="{StaticResource path}" Duration="0:0:5" RepeatBehavior="Forever" Source ="Y"/>

                    </Storyboard>

                </BeginStoryboard>

            </EventTrigger.Actions>

        </EventTrigger>

    </Window.Triggers>

    <Canvas>

        <Path Stroke="Red" StrokeThickness="1" Data="{StaticResource path}" Canvas.Top="10" Canvas.Left="10"></Path>

        <Image x:Name="image">

            <Image.Source>

                <DrawingImage>

                    <DrawingImage.Drawing>

                        <GeometryDrawing Brush="LightSkyBlue">

                            <GeometryDrawing.Geometry>

                                <GeometryGroup>

                                    <EllipseGeometry Center="10,10" RadiusX="9" RadiusY="4" ></EllipseGeometry>

                                    <EllipseGeometry Center="10,10" RadiusX="4" RadiusY="9" ></EllipseGeometry>

                                </GeometryGroup>

                            </GeometryDrawing.Geometry>

                            <GeometryDrawing.Pen>

                                <Pen Thickness="1" Brush="Black"></Pen>

                            </GeometryDrawing.Pen>

                        </GeometryDrawing>

                    </DrawingImage.Drawing>

                </DrawingImage>

            </Image.Source>

        </Image>

</Canvas>

结果如图所示:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值