WPF中的动画——(5)

   

          我们见识了普通的动画类、关键帧动画类,当然,还有一种动画类需要我们了解,那就是基于路径的动画。它有3个类型:DoubleAnimationUsingPathMatrixAniamtionUsingPathPointAnimationUsingPath

          基于路径的动画允许你指定PathGeometry作为对象行进的路径来改变对象的位置。这些路径可以是矩形、椭圆、弧形或任何组合。

          PointAniamtionUsingPath为例:

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Path Fill="Blue">

        <Path.Data>

            <EllipseGeometry x:Name="elips" RadiusX="12" RadiusY="12"/>

        </Path.Data>

        <Path.Triggers>

            <EventTrigger RoutedEvent="Path.Loaded">

                <BeginStoryboard>

                    <Storyboard TargetName="elips" TargetProperty="Center">

                        <PointAnimationUsingPath Duration="0:0:2.5" AutoReverse="True"

                                                                          RepeatBehavior="Forever">

                            <PointAnimationUsingPath.PathGeometry>

                                <PathGeometry Figures="M 96 288 C 576 0, 0 0, 480 288"/>

                            </PointAnimationUsingPath.PathGeometry>

                        </PointAnimationUsingPath>

                    </Storyboard>

                </BeginStoryboard>

            </EventTrigger>

        </Path.Triggers>

    </Path>

    <Path Stroke="Black" Data="M 96 288 C 576 0, 0 0, 480 288"/>

</Canvas>

运行起来,可以看到,球的中心,会精确地沿着定义的贝塞尔曲线移动。

如图:

         DoubleAnimationUsingPath中,你会使用到指定PathGeometryXYAngle为目标的X值、Y值以及目标的旋转角度,这样,目标不光能沿着路径前进,还能根据路径的斜率来改变自己的角度,使自己始终平行于路径。

          三个DoubleAnimationUsingPath来改变目标的水平和垂直位置并旋转角度,这样的一个效果往往可以只用一个MatrixAnimationUsingPath来实现(路径的XY会变成MatrixOffsetXOffsetYMatrix剩下的部分用来定义旋转——需要设定DoesRotateWithTangentTrue)。接下来,用骑单轮车的机器人绕行于山丘大道来为MatrixAnimationUsingPath作例子:

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Canvas.Resources>

        <PathGeometry x:Key="path"

                      Figures="M 200 200

                               C 300 0 500 400 700 200

                               C 900 0 1000 200 900 300

                               C 100 1100 1200 800 400 500

                               C 100 400 100 400 200 200"/>

        <Style TargetType="{x:Type Path}">

            <Setter Property="Stroke" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>

        </Style>

    </Canvas.Resources>

    <Path Data="{StaticResource path}"/>

    <Path>

        <Path.Data>

            <GeometryGroup>

                <EllipseGeometry Center="0 -25" RadiusX="25" RadiusY="25"/>

                <GeometryGroup>

                    <LineGeometry StartPoint="0 0" EndPoint="0 -50"/>

                    <LineGeometry StartPoint="-25 -25" EndPoint="25 -25"/>

                    <LineGeometry StartPoint="-18 -7" EndPoint="18 -43"/>

                    <LineGeometry StartPoint="18 -7" EndPoint="-18 -43"/>

                    <GeometryGroup.Transform>

                        <RotateTransform x:Name="xformSpokes"

                                         CenterX="0" CenterY="-25"/>

                    </GeometryGroup.Transform>

                </GeometryGroup>

                <LineGeometry StartPoint="0 -25" EndPoint="0 -80"/>

                <EllipseGeometry Center="0 -90" RadiusX="10" RadiusY="10"/>

                <LineGeometry StartPoint="9 -85" EndPoint="0 -90"/>

                <LineGeometry StartPoint="-35 -70" EndPoint="35 -70">

                    <LineGeometry.Transform>

                        <RotateTransform x:Name="xformArm" CenterX="0" CenterY="-70"/>

                    </LineGeometry.Transform>

                </LineGeometry>

            </GeometryGroup>

        </Path.Data>

        <Path.RenderTransform>

            <MatrixTransform x:Name="xformUnicycleMan"/>

        </Path.RenderTransform>

    </Path>

    <Canvas.Triggers>

        <EventTrigger RoutedEvent="Canvas.Loaded">

            <BeginStoryboard>

                <Storyboard SpeedRatio="1">

                    <MatrixAnimationUsingPath Storyboard.TargetName="xformUnicycleMan"

                                              Storyboard.TargetProperty="Matrix"

                                              Duration="0:0:12"

                                              PathGeometry="{StaticResource path}"

                                              DoesRotateWithTangent="True"

                                              RepeatBehavior="Forever"/>

                    <DoubleAnimation Storyboard.TargetName="xformSpokes"

                                     Storyboard.TargetProperty="Angle"

                                     Duration="0:0:1" RepeatBehavior="Forever"

                                     From="0" To="360"/>

                    <DoubleAnimation Storyboard.TargetName="xformArm"

                                     Storyboard.TargetProperty="Angle"

                                     Duration="0:0:0.2"

                                     AutoReverse="True"

                                     RepeatBehavior="Forever"

                                     From="-20" To="20"/>

                </Storyboard>

            </BeginStoryboard>

        </EventTrigger>

    </Canvas.Triggers>

</Canvas>

运行结果如图:

 

另外,可以让骑单轮车的机器人沿着文字运动,可以参考Demo。抓个图吧。

 

转载于:https://www.cnblogs.com/xirihanlin/archive/2009/03/05/1404255.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值