runxinzhi.com
首页
百度搜索
Silverlight之我见——制作星星闪烁动画
圣诞节来了,无聊,做点东西纪念一下。
原理很简单,生成1000个圆,从随机数来布置它们的位置,通过动画来处理它们的透明度,动画时长也是随机生成。
1、创建图形数组并设置背景透明,渐变笔触,大小等,而后加入到Grid元素的子元素集中;
2、创建动画时间线;
3、加载完成后播放动画;
4、每一轮动画播放完毕后,重新随机生成一下图形的Margin,动画的时间长度也是随机生成。
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace RandEllipseSample { public partial class MainPage : UserControl { int shapesCount = 500; //图形数组的容量 //随机大小的上限 int theMaxW = 1300; int theMaxH = 720; Random rand = null; Storyboard story = null; Ellipse[] myShapes = null; public MainPage() { InitializeComponent(); rand = new Random(); story = new Storyboard(); story.Completed += new EventHandler(story_Completed); InitShapes(); InitAnimation(); //加载完成后马上播放动画 this.Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { story.Begin(); } void story_Completed(object sender, EventArgs e) { for (int x = 0; x < shapesCount; x++) { myShapes[x].Margin = new Thickness(Convert.ToDouble(rand.Next(0, theMaxW)), Convert.ToDouble(rand.Next(0, theMaxH)), 0, 0); } InitAnimation(); } /// <summary> /// 初始化形状数组 /// </summary> private void InitShapes() { myShapes = new Ellipse[shapesCount]; //实例化所有成员 for (int n = 0; n < shapesCount; n++) { myShapes[n] = new Ellipse(); myShapes[n].Fill = new SolidColorBrush(Colors.Transparent); myShapes[n].StrokeThickness = 2d; //笔触为线性渐变 LinearGradientBrush gBrush = new LinearGradientBrush(); gBrush.StartPoint = new Point(0, 0); gBrush.EndPoint = new Point(1, 1); gBrush.GradientStops.Add(new GradientStop() { Color = Colors.Yellow, Offset = 0 }); gBrush.GradientStops.Add(new GradientStop() { Color = Colors.Red, Offset = 0.25 }); gBrush.GradientStops.Add(new GradientStop() { Color = Colors.White, Offset = 0.5 }); gBrush.GradientStops.Add(new GradientStop() { Color = Colors.Blue, Offset = 0.75 }); myShapes[n].Stroke = gBrush; //位置 myShapes[n].Margin = new Thickness(Convert.ToDouble(rand.Next(0,theMaxW)), Convert.ToDouble(rand.Next(0,theMaxH)), 0, 0); //大小 myShapes[n].Width = 10; myShapes[n].Height = 10; myShapes[n].HorizontalAlignment = HorizontalAlignment.Left; myShapes[n].VerticalAlignment = VerticalAlignment.Top; //加入可视化树 this.LayoutRoot.Children.Add(myShapes[n]); } } /// <summary> /// 初始化动画 /// </summary> private void InitAnimation() { story.Children.Clear(); for (int i = 0; i < shapesCount; i++) { int mSecond = rand.Next(0, 5); //透明度 DoubleAnimation opacityAnimate = new DoubleAnimation(); opacityAnimate.From = 1.0; opacityAnimate.To = 0.0; Storyboard.SetTarget(opacityAnimate, myShapes[i]); Storyboard.SetTargetProperty(opacityAnimate, new PropertyPath("Opacity")); opacityAnimate.Duration = new Duration(TimeSpan.FromSeconds(mSecond)); opacityAnimate.RepeatBehavior = RepeatBehavior.Forever; //将时间线添加到情节摘要 story.Children.Add(opacityAnimate); } } } }
相关阅读:
构建VIM下的C++编程环境
[原]在Fedora 20环境下安装系统内核源代码
[转]程序员技术练级攻略
[原]Fedora 20安装记录
【转】ODBC、OLE DB、 ADO的区别
C# & SQL Server大数据量插入方式对比
字符串散列函数示例
[转]wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容)
SOCKET:SO_LINGER 选项
TCP三次握手与四次挥手
原文地址:https://www.cnblogs.com/javawebsoa/p/2457960.html
最新文章
SlickGrid example 3: 可编辑单元
SlickGrid example 3b: 支持撤销操作的编辑单元
SlickGrid example 3a: 可编辑单元
SlickGrid example 2: 按想要的风格展示列
SlickGrid example 1: 最简单的例子和用法
jQuery.validator 详解
javascript事件处理解析
jQuery.validator 验证规则详解
Javascript中自动切换焦点
Javascript事件委托
热门文章
Tomcat
xargs
tcpdump命令使用详解
sar命令使用详解
mbr 备份
linux系统优化
host dig nslookup bind
mysql 授权新的root用户
linux find查找并拷贝 exec xargs区别
TCP三次握手与DDOS攻击原理
Copyright © 2020-2023
润新知