一般为了用户界面应用动画,只不过是创建并配置正确的动画和故事版对象。但在其他情况下,特别是同时发生多个动画时,可能更加需要关注性能。WPF试图保持以60帧/秒的速度进行动画,可以确保从开始到结束得到平滑流畅的动画。帧速率越低,会发生抖动现象。帧速率越高,占用的CPU也就越高。通过‘TimeLine.DesiredFrameRate’属性进行调整帧率。‘TimeLine’类,TimeLine时间轴是Flash的一大特点,在以往的动画制作中,通常是要绘制作出每一帧的图像,或是通过程序来制作,而Flash使用关键帧技术,通过对时间轴上的关键帧的制作,Flash会自动生成运动中的动画帧,节省了制作人员的大部份的时间,也提高了效率。在时间轴的上面有一个红色的线,那是播放的定位磁头,拖动磁头也可以实现动画的观察,这在制作当中是很重要的步骤。‘DesiredFrameRate ’属性用于定义动画应在其中运行的特定帧速率。 这仅是一个指导原则,不是有保证的值。 该属性具有两个主要用途,第一是限制缓慢移动,背景类型的动画不需要高度的保真度的处理资源的量。 这可以通过在时间线上设置小帧速率值来实现;第二是减少上快速移动的水平动画撕裂现象的视觉效果。 这可以通过在时间线上设置高的帧速率值来实现。仅为根时间线上设置此属性。
下面是一个例子代码:
<Window x:Class=“动画.动画的帧率”
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:动画"
mc:Ignorable="d"
Title="动画的帧率" Height="500" Width="300">
<Window.Triggers>
<EventTrigger RoutedEvent="Button.Click" SourceName="btn_start">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="ellipse1" Storyboard.TargetProperty="(Canvas.Left)" From="0" To="300" Duration="0:0:5"/>
<DoubleAnimation Storyboard.TargetName="ellipse2" Storyboard.TargetProperty="(Canvas.Left)" From="0" To="250" Duration="0:0:5"/>
<DoubleAnimation Storyboard.TargetName="ellipse3" Storyboard.TargetProperty="(Canvas.Left)" From="0" To="200" Duration="0:0:5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Window.Triggers>
<Grid ShowGridLines="False">
<Grid.RowDefinitions>
<RowDefinition Height="5*"/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions >
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Canvas ClipToBounds="True" Grid.Row="0" Grid.ColumnSpan="2" Height="320" Background="Black">
<Ellipse Name="ellipse1" Fill="Red" Width="60" Height="60"></Ellipse>
<Ellipse Name="ellipse2" Margin="0,80,0,0" Fill="Green" Width="60" Height="60"></Ellipse>
<Ellipse Name="ellipse3" Margin="0,170,0,0" Fill="Yellow" Width="60" Height="60"></Ellipse>
</Canvas>
<Label Grid.Row="1" Content="帧速率:" FontSize="20" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<TextBox Name="txtBox1" Text="1" Width="60" Height="30" FontSize="20" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Button Name="btn_start" Grid.Row="2" Grid.ColumnSpan="2" Width="200" Height="60" Content="点击开始" FontSize="20"></Button>
</Grid>
</Window>
下面是结果图