WPF 路径动画PathAnimations的使用

在wpf中让一个控件按照一定的路径运行的动画。。叫做路径动画,这个示例演示了让一个rectangle按照一个s形曲线反复运行的动画。

效果:

只有一个文件:

全部代码如下 :

<Window x:Class="wpfcore.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:local="clr-namespace:wpfcore"
        mc:Ignorable="d"
        Background="LightBlue"
        Title="MainWindow" Width="420" Height="340">
    <Window.Resources>
        <PathGeometry x:Key="SLine" Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" />
    </Window.Resources>
    <Grid Margin="20">
        <StackPanel>


        <Canvas HorizontalAlignment="Left" Margin="0" Width="340" Height="215" >


        <Path VerticalAlignment="Top" Margin="15,15,15,15" 
              Data="{StaticResource SLine}"
              Stroke="Blue" StrokeThickness="2"
              Stretch="None"/>


      <Path Fill="Red">
        <Path.Data>
        <RectangleGeometry x:Name="MyAnimatedRectGeometry" Rect="0,0 30 30" >
            <RectangleGeometry.Transform>
                <TransformGroup>
                <RotateTransform x:Name="MyRotateTransform" Angle="0" CenterX="15" CenterY="15" />
                <TranslateTransform x:Name="MyTranslateTransform" X="10" Y="100" />
                </TransformGroup>
            </RectangleGeometry.Transform>
        </RectangleGeometry>
        </Path.Data>
      </Path>
            </Canvas>
  <Button Width="80" HorizontalAlignment="Left" Content="开始动画">
      <Button.Triggers>
      <EventTrigger RoutedEvent="Button.Click">
        <BeginStoryboard Name="MyBeginStoryboard">
            <Storyboard>
                <DoubleAnimationUsingPath
                    Storyboard.TargetName="MyRotateTransform"
                    Storyboard.TargetProperty="Angle"
                    Source="Angle" 
                    Duration="0:0:5" 
                    RepeatBehavior="Forever" AutoReverse="True" 
                    PathGeometry="{StaticResource SLine}">
                </DoubleAnimationUsingPath>
                <DoubleAnimationUsingPath
                    Storyboard.TargetName="MyTranslateTransform"
                    Storyboard.TargetProperty="X"
                    Source="X" 
                    Duration="0:0:5" 
                    RepeatBehavior="Forever" AutoReverse="True"  PathGeometry="{StaticResource SLine}">
                </DoubleAnimationUsingPath>


                <DoubleAnimationUsingPath
                    Storyboard.TargetName="MyTranslateTransform"
                    Storyboard.TargetProperty="Y"
                    Source="Y" 
                    Duration="0:0:5" 
                    RepeatBehavior="Forever" AutoReverse="True"  PathGeometry="{StaticResource SLine}">
                </DoubleAnimationUsingPath>


            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
      </Button.Triggers>  
  </Button>
        </StackPanel>
    </Grid>
</Window>


动画截图:

完事,搞定。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值