技术开发 频道

WPF中的动画

[IT168技术文档]动画无疑是WPF中最吸引人的特色之一,其可以像Flash一样平滑地播放并与程序逻辑进行很好的交互。这里我们讨论一下故事板。

在WPF中我们采用Storyboard(故事板)的方式来编写动画,为了对Storyboard有个大概的印象,你可以粘贴以下代码到XamlPad来查看效果:
<!-- This example shows how to animate with a storyboard.--> <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="Storyboards Example"> <StackPanel Margin="20"> <Rectangle Name="MyRectangle" Width="100" Height="100"> <Rectangle.Fill> <SolidColorBrush x:Name="MySolidColorBrush" Color="Blue" /> </Rectangle.Fill> <Rectangle.Triggers> <EventTrigger RoutedEvent="Page.Loaded"> <BeginStoryboard> <Storyboard RepeatBehavior="Forever" AutoReverse="True"> <DoubleAnimation Storyboard.TargetName="MyRectangle" Storyboard.TargetProperty="Width" From="100" To="200" Duration="0:0:1" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> </StackPanel> </Page>

在介绍Storyboard之前应该先了解Animation
Animation提供一种简单的“渐变”动画,我们为一个Animation指定开始值和一个结束值,并指定由开始值到达结束值所需的时间,便可形成一个简单的动画。比如我们指定长方形的宽度由100变化到200,所需时间为1秒,很容易想像这样的动画是什么样的,而它对应的Xaml代码如下:

<DoubleAnimation Storyboard.TargetName="MyRectangle" Storyboard.TargetProperty="Width" From="100" To="200" Duration="0:0:1" />

将它翻译成C#代码则如下:

DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 100; myDoubleAnimation.To = 200; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name); Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty));

代码里我们定义了一个DoubleAnimation,并指定了它的开始值和结束值以及它由开始值到达结束值所需的时间。至于后面两句,它们是用来将Aniamtion与指定的对象和指定的属性相关联,等会我们将介绍。
注意到,这里我们使用的是DoubleAnimation,因为我们所要变化的是数值。那么如果我们要变化颜色是不是就用ColorAnimation了呢,对,其实出了这些之外还有PointAnimation等等,并且你可以实现IAnimatable接口来实现自定义版本的Animation。关于这些你可以参见System.Windows.MediaAniamtion名字空间.

但值得注意的是并非每个属性都能够使用Animation,它必须满足以下条件:
1,它必须是Dependency Property
2,它所在类必须继承于DependencyObject,必须实现了IAnimatable接口.
3,必须有类型一致的Animation Type(即Color类型使用ColorAniamtion,Point类型使用PointAnimation等)

一个简单的Animation定义了一个简单的动画,很容易想到的是,如果若干个Animation同时作用于一个对象,那么这个对象不就可以表现复杂的动画了吗,对,这就是Storyboard

Storyboard可以看做是Animation的容器,它包含了若干的简单动画来完成一个复杂动画。
参考以下代码:

<!-- This example shows how to animate with a storyboard.--> <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="Storyboards Example"> <StackPanel Margin="20"> <Rectangle Name="MyRectangle" Width="100" Height="100"> <Rectangle.Fill> <SolidColorBrush x:Name="MySolidColorBrush" Color="Blue" /> </Rectangle.Fill> <Rectangle.Triggers> <EventTrigger RoutedEvent="Page.Loaded"> <BeginStoryboard> <Storyboard RepeatBehavior="Forever" AutoReverse="True"> <DoubleAnimation Storyboard.TargetName="MyRectangle" Storyboard.TargetProperty="Width" From="100" To="200" Duration="0:0:1" /> <ColorAnimation Storyboard.TargetName="MySolidColorBrush" Storyboard.TargetProperty="Color" From="Blue" To="Red" Duration="0:0:1" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> </StackPanel> </Page>

这里我们的Storyboard定义了DoubleAnimation来变化矩形的宽度,并定义了ColorAnimation来变化矩形的颜色。

至此,你已经可以编写绚丽的WPF动画了,并推荐你下载Expression Blend来制作WPF动画.

 

0
相关文章