Silverlight是基于时间线的,不象Flash是基于帧的,所以在Silverlight中,很少看到有文档专门介绍SL中的帧。
但是我们从动画原理知道,动画只不过是一幅幅静态图片连续播放,利用人眼的视觉暂留形成的,因此任何动画从原理上讲,至少还是有每秒播放多少帧这个概念的。
Silverlight的sdk文档中,有一段话:
...
maxFramerate 值可通过 Silverlight 插件对象的 maxframerate 参数进行配置。maxframerate 参数的默认值为 60。currentFramerate 和 maxFramerate 是报告每秒帧数 (fps) 的值。实际显示的帧速率设置为较低的数字。可以通过特意设置一个较低的 maxframerate 值(如 2,每秒 2 帧)来阐述 currentFramerate 与 maxFramerate 之间的关系。
...
即sl每秒种默认最多播放60帧,当然我们也能用代码来改变该值(比如设置到100),但最终sl的当前播放速度与硬件有关,并不是你想设多高就能达到多高。
测试xaml:
代码 by 菩提树下的杨过 http://yjmyzz.cnblogs.com/
<UserControl x:Class="CompositionTarget_Rendering.MainPage"
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"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<StackPanel x:Name="LayoutRoot" HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock Text="ready" x:Name="txt" HorizontalAlignment="Center"></TextBlock>
<StackPanel Margin="5">
<Slider Width="200" Minimum="1" Maximum="100" x:Name="slider" ValueChanged="slider_ValueChanged" Value="60" ></Slider>
<TextBlock x:Name="txtMax" Margin="0,5,0,0" HorizontalAlignment="Center" Text="60" ></TextBlock>
</StackPanel>
</StackPanel>
</UserControl>
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"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<StackPanel x:Name="LayoutRoot" HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock Text="ready" x:Name="txt" HorizontalAlignment="Center"></TextBlock>
<StackPanel Margin="5">
<Slider Width="200" Minimum="1" Maximum="100" x:Name="slider" ValueChanged="slider_ValueChanged" Value="60" ></Slider>
<TextBlock x:Name="txtMax" Margin="0,5,0,0" HorizontalAlignment="Center" Text="60" ></TextBlock>
</StackPanel>
</StackPanel>
</UserControl>
sl中有一个CompositionTarget.Rendering事件可以在每帧进入(刚开始播放)时触发,测试cs代码:
代码 by 菩提树下的杨过 http://yjmyzz.cnblogs.com/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
namespace CompositionTarget_Rendering
{
public partial class MainPage : UserControl
{
bool isLoaded = false;
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
isLoaded = true;
}
/// <summary>
/// 该事件在进入每一帧时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void CompositionTarget_Rendering(object sender, EventArgs e)
{
this.txt.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
}
private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (isLoaded)
{
Settings settings = Application.Current.Host.Settings;
settings.EnableFrameRateCounter = true;
settings.MaxFrameRate = (int)slider.Value;
txtMax.Text = settings.MaxFrameRate.ToString();
}
}
}
}
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
namespace CompositionTarget_Rendering
{
public partial class MainPage : UserControl
{
bool isLoaded = false;
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
isLoaded = true;
}
/// <summary>
/// 该事件在进入每一帧时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void CompositionTarget_Rendering(object sender, EventArgs e)
{
this.txt.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
}
private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (isLoaded)
{
Settings settings = Application.Current.Host.Settings;
settings.EnableFrameRateCounter = true;
settings.MaxFrameRate = (int)slider.Value;
txtMax.Text = settings.MaxFrameRate.ToString();
}
}
}
}
进入每帧时,我们让txt显示当前时间,同时拖动滑块,还能设置当前动画每秒播放的最大帧数,运行效果: