本文实例为大家分享了silverlight实现星星闪烁动画展示的具体代码,供大家参考,具体内容如下
原理很简单,生成1000个圆,从随机数来布置它们的位置,通过动画来处理它们的透明度,动画时长也是随机生成。
1、创建图形数组并设置背景透明,渐变笔触,大小等,而后加入到grid元素的子元素集中;
2、创建动画时间线;
3、加载完成后播放动画;
4、每一轮动画播放完毕后,重新随机生成一下图形的margin,动画的时间长度也是随机生成。
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
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);
}
}
}
}
|
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/tcjiaan/article/details/7101546