日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯網營銷解決方案
通過WPF實現文字粒子閃爍

本篇文章重點為大家講解一下通過WPF實現文字粒子閃爍具體方法,有需要的小伙伴可以參考一下。

成都創(chuàng)新互聯堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都做網站、成都網站建設、企業(yè)官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯網時代的西城網站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!

實現效果如下:

思路:

首先根據顯示文本創(chuàng)建文本路徑Geometry,然后在路徑內隨機生成圓形粒子并添加動畫。

步驟:

1、粒子類Particle.cs

public class Particle
{
   ///
   /// 形狀
   ///
   public Ellipse Shape;
   ///
   /// 坐標
   ///
   public Point Position;
}

2、粒子系統ParticleSystem.cs

///
/// 粒子路徑
///
private Geometry particleGeometry;
 
///
/// 粒子個數
///
private int particleCount = 100;
 
///
/// 粒子最小尺寸
///
private static int sizeMin = 10;
 
///
/// 粒子最大尺寸
///
private int sizeMax = 20;
 
///
/// 隨機數
///
private Random random;
 
///
/// 粒子列表
///
private List particles;
 
///
/// 粒子容器
///
private Canvas containerParticles;
 
 
   public ParticleSystem(Geometry _path, int _maxRadius, int _particleCount, Canvas _containerParticles)
   {
     particleGeometry = _path;
     particleCount = _particleCount;
     sizeMax = _maxRadius;
     containerParticles = _containerParticles;
     random = new Random();
     particles = new List();
     SpawnParticle();
   }
 
   ///
   /// 初始化粒子
   ///
   private void SpawnParticle()
   {
     //清空粒子隊列
     particles.Clear();
     containerParticles.Children.Clear();
 
     //生成粒子
     for (int i = 0; i while(true)
       {
         Point po = new Point(random.Next((int)particleGeometry.Bounds.Left, (int)particleGeometry.Bounds.Right), random.Next((int)particleGeometry.Bounds.Top, (int)particleGeometry.Bounds.Bottom));
         if (particleGeometry.FillContains(po, 2, ToleranceType.Absolute))
         {
           Particle p = new Particle
           {
             Shape = new Ellipse
             {
               Width = size,
               Height = size,
               Stretch = System.Windows.Media.Stretch.Fill,
               Fill = GetRandomColorBursh(),
             },
             Position = po,
           };
           SetParticleSizeAnimation(p.Shape);
           particles.Add(p);
           Canvas.SetLeft(p.Shape, p.Position.X);
           Canvas.SetTop(p.Shape, p.Position.Y);
           containerParticles.Children.Add(p.Shape);
           break;
         }
       }
     }
   }
 
   ///
   /// 設置粒子大小動畫
   ///
   private void SetParticleSizeAnimation(Ellipse p)
   {
     Storyboard sb = new Storyboard();
     //動畫完成事件 再次設置此動畫
     sb.Completed += (S, E) =>
     {
       SetParticleSizeAnimation(p);
     };
     int size = random.Next(sizeMin, sizeMax + 1);
     int time = random.Next(100, 1000);
     DoubleAnimation daX = new DoubleAnimation(size, new Duration(TimeSpan.FromMilliseconds(time)));
     DoubleAnimation daY = new DoubleAnimation(size, new Duration(TimeSpan.FromMilliseconds(time)));
     Storyboard.SetTarget(daX, p);
     Storyboard.SetTarget(daY, p);
     Storyboard.SetTargetProperty(daX, new PropertyPath("Width"));
     Storyboard.SetTargetProperty(daY, new PropertyPath("Height"));
     sb.Children.Add(daX);
     sb.Children.Add(daY);
     sb.Begin();
   }
 
   ///
   /// 獲取隨機顏色畫刷
   ///
   private SolidColorBrush GetRandomColorBursh()
   {
     byte r = (byte)random.Next(128, 256);
     byte g = (byte)random.Next(128, 256);
     byte b = (byte)random.Next(128, 256);
     return new SolidColorBrush(Color.FromArgb(125, r, g, b));
}

3、主窗體交互

private ParticleSystem ps;
 
public MainWindow()
   {
     InitializeComponent();
     this.Loaded += MainWindow_Loaded;
   }
 
   private void MainWindow_Loaded(object sender, RoutedEventArgs e)
   {
     Geometry g = CreateTextPath("H E L L O", new Point(this.cvs_particleContainer.Margin.Left, this.cvs_particleContainer.Margin.Top), new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal), 200);
     ps = new ParticleSystem(g, 25, 350, this.cvs_particleContainer);
   }
 
   ///
   /// 創(chuàng)建文本路徑
   ///
   /// 文本字符串
   /// 顯示位置
   /// 字體信息
   /// 字體大小
   ///
   private Geometry CreateTextPath(string word, Point point, Typeface typeface, int fontSize)
   {
     FormattedText text = new FormattedText(word, new System.Globalization.CultureInfo("en-US"), FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black);
     Geometry g = text.BuildGeometry(point);
     PathGeometry path = g.GetFlattenedPathGeometry();
     return path;
}

網站欄目:通過WPF實現文字粒子閃爍
網頁鏈接:http://www.5511xx.com/article/cosipeg.html