Silverlight放烟花,祝所有程序员新年快乐!!

      新的一年即将到来,我们也老了一岁,闲来无事,放个烟花庆祝自己又老了一岁,当然,除此之外还是有很多可以庆祝的。

      多余的话就不多说了,主要实现原理参考了JS放烟花的代码,但是烟花的播放自然是采用了Storyboard类啦~~,烟花就是一个Ellipse随机填充了一幅烟花效果图。实现过程相当简单,我也上传了代码,并且贴出主要的代码段:

构造烟花对象:

构造烟花对象
                ImageBrush imagebrush = new ImageBrush();
                Uri uri 
= new Uri(uris[random.Next(uris.Count)], UriKind.Relative);
                imagebrush.ImageSource 
= new BitmapImage(uri);
                ellipse.Fill 
= imagebrush;
                ellipse.Visibility 
= Visibility.Visible;
                ellipse.Width 
= 10;
                ellipse.Height 
= 10;
                ellipse.SetValue(Canvas.LeftProperty, 
this.X - ellipse.Width / 2);
                ellipse.SetValue(Canvas.TopProperty, 
this.Y);
                ((Canvas)
this.Parent).Children.Add(ellipse);
                RandomPosition(ellipse);

 

开始播放烟花:

播放烟花
public void RandomPosition(Ellipse ellipse)
        {
            Storyboard mystoryboard 
= new Storyboard();
            mystoryboard.Completed 
+= new EventHandler(mystoryboard_Completed);
            Duration dura 
= new Duration(new TimeSpan(000000001000));

            
#region 放大渐变
            DoubleAnimation dua1 
= new DoubleAnimation();
            dua1.Duration 
= dura;
            dua1.From 
= ellipse.Width;
            dua1.To 
= 250;

            DoubleAnimation dua2 
= new DoubleAnimation();
            dua2.Duration 
= dura;
            dua2.From 
= ellipse.Height;
            dua2.To 
= 250;

            Storyboard.SetTarget(dua1, ellipse);
            Storyboard.SetTargetProperty(dua1, 
new PropertyPath("(Ellipse.Width)"));
            Storyboard.SetTarget(dua2, ellipse);
            Storyboard.SetTargetProperty(dua2, 
new PropertyPath("(Ellipse.Height)"));
            mystoryboard.Children.Add(dua1);
            mystoryboard.Children.Add(dua2);
            
#endregion

            
#region 透明渐变
            DoubleAnimation dua3 
= new DoubleAnimation();
            dua3.Duration 
= dura;
            dua3.From 
= 1;
            dua3.To 
= 0;
            Storyboard.SetTarget(dua3, ellipse);
            Storyboard.SetTargetProperty(dua3, 
new PropertyPath("(Ellipse.Opacity)"));
            mystoryboard.Children.Add(dua3);
            
#endregion

            
#region 位置渐变
            DoubleAnimation dua4 
= new DoubleAnimation();
            dua4.Duration 
= dura;
            dua4.From 
= this.X - ellipse.Width / 2;
            dua4.To 
= this.X - ellipse.Width - 100;
            Storyboard.SetTarget(dua4, ellipse);
            Storyboard.SetTargetProperty(dua4, 
new PropertyPath("(Canvas.Left)"));
            mystoryboard.Children.Add(dua4);

            DoubleAnimation dua5 
= new DoubleAnimation();
            dua5.Duration 
= dura;
            dua5.From 
= this.Y;
            dua5.To 
= this.Y / 2 / 2;
            Storyboard.SetTarget(dua5, ellipse);
            Storyboard.SetTargetProperty(dua5, 
new PropertyPath("(Canvas.Top)"));
            mystoryboard.Children.Add(dua5);
            
#endregion
            mystoryboard.Begin();
        }

 项目源代码下载

 

效果来了~~~

posted @ 2009-12-26 16:40  Ron.Liang  阅读(2758)  评论(12编辑  收藏  举报