Silverlight制作逐帧动画 v2 - part2
在这里完善了一下算法,加入了fps的机制进去。
代码语言:js复制 private string[] shippics = new string[] { "Ship1/1.png", "Ship1/2.png", "Ship1/3.png", "Ship1/4.png" };
protected DateTime lastTick;
protected TimeSpan elapsed;
private double lasrFrameTime = 0;
private int currentFrame = 0;
private double fps = 15;
public MainPage()
{
InitializeComponent();
lastTick = DateTime.Now;
CompositionTarget.Rendering = new EventHandler(CompositionTarget_Rendering);
}
void CompositionTarget_Rendering(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
elapsed = now - lastTick;
lastTick = DateTime.Now;
lasrFrameTime = elapsed.TotalSeconds;
if (lasrFrameTime >( 1 / fps))
{
lasrFrameTime = 0;
currentFrame ;
currentFrame %= shippics.Length;
RenderShip();
}
}
private void RenderShip()
{
var imageBrush = new ImageBrush();
imageBrush.Stretch = Stretch.None;
imageBrush.AlignmentX = AlignmentX.Left;
imageBrush.AlignmentY = AlignmentY.Top;
imageBrush.ImageSource = new BitmapImage(new Uri("/slShipAnimate;component/" shippics[currentFrame], UriKind.Relative));
myShip.Fill = imageBrush;
}