这篇文章主要为大家详细介绍了Silverlight实现星星闪烁动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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);
}
}
}
}
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:Silverlight实现星星闪烁动画
基础教程推荐
猜你喜欢
- unity实现动态排行榜 2023-04-27
- C# List实现行转列的通用方案 2022-11-02
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C#控制台实现飞行棋小游戏 2023-04-22
- 一个读写csv文件的C#类 2022-11-06
- C# 调用WebService的方法 2023-03-09
- C# windows语音识别与朗读实例 2023-04-27
- ZooKeeper的安装及部署教程 2023-01-22
- C#类和结构详解 2023-05-30
- winform把Office转成PDF文件 2023-06-14