欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Silverlight实现星星闪烁动画

程序员文章站 2024-02-11 21:46:04
本文实例为大家分享了silverlight实现星星闪烁动画展示的具体代码,供大家参考,具体内容如下 原理很简单,生成1000个圆,从随机数来布置它们的位置,通过动画来...

本文实例为大家分享了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实现星星闪烁动画

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。