Silverlight實現(xiàn)星星閃爍動畫

本文實例為大家分享了Silverlight實現(xiàn)星星閃爍動畫展示的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)公司是一家專業(yè)提供陽新企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)H5響應式網(wǎng)站、小程序制作等業(yè)務。10年已為陽新眾多企業(yè)、政府機構(gòu)等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進行中。

原理很簡單,生成1000個圓,從隨機數(shù)來布置它們的位置,通過動畫來處理它們的透明度,動畫時長也是隨機生成。 

1、創(chuàng)建圖形數(shù)組并設(shè)置背景透明,漸變筆觸,大小等,而后加入到Grid元素的子元素集中;
2、創(chuàng)建動畫時間線;
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; //圖形數(shù)組的容量
    //隨機大小的上限
    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>
    /// 初始化形狀數(shù)組
    /// </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; 
 
        //將時間線添加到情節(jié)摘要
        story.Children.Add(opacityAnimate);
      }
    }
  }
}

效果圖:

Silverlight實現(xiàn)星星閃爍動畫

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當前名稱:Silverlight實現(xiàn)星星閃爍動畫
文章起源:http://muchs.cn/article18/gdscdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站制作網(wǎng)站建設(shè)、網(wǎng)站導航品牌網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司