• Silverlight之我见——制作星星闪烁动画


      

    圣诞节来了,无聊,做点东西纪念一下。
    原理很简单,生成1000个圆,从随机数来布置它们的位置,通过动画来处理它们的透明度,动画时长也是随机生成。
    1、创建图形数组并设置背景透明,渐变笔触,大小等,而后加入到Grid元素的子元素集中;
    2、创建动画时间线;
    3、加载完成后播放动画;
    4、每一轮动画播放完毕后,重新随机生成一下图形的Margin,动画的时间长度也是随机生成。
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Net;  
    5. using System.Windows;  
    6. using System.Windows.Controls;  
    7. using System.Windows.Documents;  
    8. using System.Windows.Input;  
    9. using System.Windows.Media;  
    10. using System.Windows.Media.Animation;  
    11. using System.Windows.Shapes;   
    12.   
    13. namespace RandEllipseSample  
    14. {  
    15.     public partial class MainPage : UserControl  
    16.     {  
    17.         int shapesCount = 500; //图形数组的容量  
    18.         //随机大小的上限  
    19.         int theMaxW = 1300;  
    20.         int theMaxH = 720;  
    21.         Random rand = null;  
    22.         Storyboard story = null;  
    23.         Ellipse[] myShapes = null;  
    24.         public MainPage()  
    25.         {  
    26.             InitializeComponent();  
    27.             rand = new Random();  
    28.             story = new Storyboard();  
    29.             story.Completed += new EventHandler(story_Completed);  
    30.             InitShapes();  
    31.             InitAnimation();  
    32.             //加载完成后马上播放动画  
    33.             this.Loaded += new RoutedEventHandler(MainPage_Loaded);  
    34.         }   
    35.   
    36.         void MainPage_Loaded(object sender, RoutedEventArgs e)  
    37.         {  
    38.             story.Begin();  
    39.         }   
    40.   
    41.         void story_Completed(object sender, EventArgs e)  
    42.         {  
    43.             for (int x = 0; x < shapesCount; x++)  
    44.             {  
    45.                 myShapes[x].Margin = new Thickness(Convert.ToDouble(rand.Next(0, theMaxW)), Convert.ToDouble(rand.Next(0, theMaxH)), 0, 0);  
    46.             }  
    47.             InitAnimation();  
    48.         }   
    49.   
    50.         /// <summary>  
    51.         /// 初始化形状数组  
    52.         /// </summary>  
    53.         private void InitShapes()  
    54.         {  
    55.             myShapes = new Ellipse[shapesCount];  
    56.             //实例化所有成员  
    57.             for (int n = 0; n < shapesCount; n++)  
    58.             {  
    59.                 myShapes[n] = new Ellipse();  
    60.                 myShapes[n].Fill = new SolidColorBrush(Colors.Transparent);  
    61.                 myShapes[n].StrokeThickness = 2d;  
    62.                 //笔触为线性渐变  
    63.                 LinearGradientBrush gBrush = new LinearGradientBrush();  
    64.                 gBrush.StartPoint = new Point(0, 0);  
    65.                 gBrush.EndPoint = new Point(1, 1);  
    66.                 gBrush.GradientStops.Add(new GradientStop()  
    67.                 {  
    68.                     Color = Colors.Yellow,  
    69.                     Offset = 0  
    70.                 });  
    71.                 gBrush.GradientStops.Add(new GradientStop()  
    72.                 {  
    73.                     Color = Colors.Red,  
    74.                     Offset = 0.25  
    75.                 });  
    76.                 gBrush.GradientStops.Add(new GradientStop()  
    77.                 {  
    78.                     Color = Colors.White,  
    79.                     Offset = 0.5  
    80.                 });  
    81.                 gBrush.GradientStops.Add(new GradientStop()  
    82.                 {  
    83.                     Color = Colors.Blue,  
    84.                     Offset = 0.75  
    85.                 });  
    86.                 myShapes[n].Stroke = gBrush;  
    87.                 //位置  
    88.                 myShapes[n].Margin = new Thickness(Convert.ToDouble(rand.Next(0,theMaxW)), Convert.ToDouble(rand.Next(0,theMaxH)), 0, 0);  
    89.                 //大小  
    90.                 myShapes[n].Width = 10;  
    91.                 myShapes[n].Height = 10;  
    92.                 myShapes[n].HorizontalAlignment = HorizontalAlignment.Left;  
    93.                 myShapes[n].VerticalAlignment = VerticalAlignment.Top;  
    94.                 //加入可视化树  
    95.                 this.LayoutRoot.Children.Add(myShapes[n]);  
    96.             }  
    97.         }   
    98.   
    99.         /// <summary>  
    100.         /// 初始化动画  
    101.         /// </summary>  
    102.         private void InitAnimation()  
    103.         {  
    104.             story.Children.Clear();  
    105.             for (int i = 0; i < shapesCount; i++)  
    106.             {  
    107.                 int mSecond = rand.Next(0, 5);  
    108.                 //透明度  
    109.                 DoubleAnimation opacityAnimate = new DoubleAnimation();  
    110.                 opacityAnimate.From = 1.0;  
    111.                 opacityAnimate.To = 0.0;  
    112.                 Storyboard.SetTarget(opacityAnimate, myShapes[i]);  
    113.                 Storyboard.SetTargetProperty(opacityAnimate,  
    114.                     new PropertyPath("Opacity"));  
    115.                 opacityAnimate.Duration = new Duration(TimeSpan.FromSeconds(mSecond));  
    116.                 opacityAnimate.RepeatBehavior = RepeatBehavior.Forever;   
    117.   
    118.                 //将时间线添加到情节摘要  
    119.                 story.Children.Add(opacityAnimate);  
    120.             }  
    121.         }  
    122.     }  
    123. }  

  • 相关阅读:
    Ubuntu 12.04下GAMIT10.40安装说明
    GAMIT 10.50在Ubuntu 12.04系统下的安装
    tomcat 5.5 动态加载类
    GAMIT 10.50在Ubuntu 12.04系统下的安装
    RHCE 系列(九):如何使用无客户端配置 Postfix
    Nginx+Keepalived(带Nginx监控脚本)
    黑马程序员_java08_多线程
    oracle 表类型变量的使用
    如何在win7系统中安装redis
    bzoj 2816: [ZJOI2012]网络(splay)
  • 原文地址:https://www.cnblogs.com/xieweikai/p/6832796.html
Copyright © 2020-2023  润新知