欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF實(shí)現(xiàn)文字粒子閃爍動(dòng)畫效果

 更新時(shí)間:2020年08月28日 17:30:44   作者:RunnerDNA  
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)文字粒子閃爍動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了WPF實(shí)現(xiàn)文字粒子閃爍動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)效果如下:

思路:首先根據(jù)顯示文本創(chuàng)建文本路徑Geometry,然后在路徑內(nèi)隨機(jī)生成圓形粒子并添加動(dòng)畫。

步驟:

1、粒子類Particle.cs

public class Particle
 {
  /// <summary>
  /// 形狀
  /// </summary>
  public Ellipse Shape;
  /// <summary>
  /// 坐標(biāo)
  /// </summary>
  public Point Position;
}

2、粒子系統(tǒng)ParticleSystem.cs

/// <summary>
/// 粒子路徑
/// </summary>
private Geometry particleGeometry;
 
/// <summary>
/// 粒子個(gè)數(shù)
/// </summary>
private int particleCount = 100;
 
/// <summary>
/// 粒子最小尺寸
/// </summary>
private static int sizeMin = 10;
 
/// <summary>
/// 粒子最大尺寸
/// </summary>
private int sizeMax = 20;
 
/// <summary>
/// 隨機(jī)數(shù)
/// </summary>
private Random random;
 
/// <summary>
/// 粒子列表
/// </summary>
private List<Particle> particles;
 
/// <summary>
/// 粒子容器
/// </summary>
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<Particle>();
   SpawnParticle();
  }
 
  /// <summary>
  /// 初始化粒子
  /// </summary>
  private void SpawnParticle()
  {
   //清空粒子隊(duì)列
   particles.Clear();
   containerParticles.Children.Clear();
 
   //生成粒子
   for (int i = 0; i < particleCount; i++)
   {
    double size = random.Next(sizeMin, sizeMax + 1);
    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;
     }
    }
   }
  }
 
  /// <summary>
  /// 設(shè)置粒子大小動(dòng)畫
  /// </summary>
  private void SetParticleSizeAnimation(Ellipse p)
  {
   Storyboard sb = new Storyboard();
   //動(dòng)畫完成事件 再次設(shè)置此動(dòng)畫
   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();
  }
 
  /// <summary>
  /// 獲取隨機(jī)顏色畫刷
  /// </summary>
  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);
  }
 
  /// <summary>
  /// 創(chuàng)建文本路徑
  /// </summary>
  /// <param name="word">文本字符串</param>
  /// <param name="point">顯示位置</param>
  /// <param name="typeface">字體信息</param>
  /// <param name="fontSize">字體大小</param>
  /// <returns></returns>
  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;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。 

相關(guān)文章

  • C#測(cè)量程序運(yùn)行時(shí)間及cpu使用時(shí)間實(shí)例方法

    C#測(cè)量程序運(yùn)行時(shí)間及cpu使用時(shí)間實(shí)例方法

    對(duì)一個(gè)服務(wù)器程序想統(tǒng)計(jì)每秒可以處理多少數(shù)據(jù)包,要如何做?答案是用處理數(shù)據(jù)包的總數(shù),除以累記處理數(shù)據(jù)包用的時(shí)間,下面我們看一個(gè)代碼實(shí)例就明白了
    2013-11-11
  • C#拆分字符串正則表達(dá)式Regex.Split和String.Split方法

    C#拆分字符串正則表達(dá)式Regex.Split和String.Split方法

    這篇文章主要給大家介紹了關(guān)于C#拆分字符串正則表達(dá)式Regex.Split和String.Split方法的相關(guān)資料,在C#中,Regex.Split方法和string.Split方法都用于分割字符串,但它們有一些重要的區(qū)別,文中通過(guò)代碼詳細(xì)講解下,需要的朋友可以參考下
    2024-04-04
  • C#實(shí)現(xiàn)DataTable數(shù)據(jù)行列轉(zhuǎn)換

    C#實(shí)現(xiàn)DataTable數(shù)據(jù)行列轉(zhuǎn)換

    這篇文章介紹了C#實(shí)現(xiàn)DataTable數(shù)據(jù)行列轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器

    C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器

    這篇文章主要介紹了C#實(shí)現(xiàn)Ruby的負(fù)數(shù)索引器的相關(guān)代碼和使用方法,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2016-07-07
  • 對(duì)指定的網(wǎng)頁(yè)進(jìn)行截圖的效果 C#版

    對(duì)指定的網(wǎng)頁(yè)進(jìn)行截圖的效果 C#版

    對(duì)指定的網(wǎng)頁(yè)進(jìn)行截圖的效果 C#版...
    2007-08-08
  • Winform自定義控件在界面拖動(dòng)、滾動(dòng)鼠標(biāo)時(shí)閃爍的解決方法

    Winform自定義控件在界面拖動(dòng)、滾動(dòng)鼠標(biāo)時(shí)閃爍的解決方法

    這篇文章介紹了Winform自定義控件在界面拖動(dòng)、滾動(dòng)鼠標(biāo)時(shí)閃爍的解決方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • c#連接mdf文件示例分享

    c#連接mdf文件示例分享

    這篇文章主要介紹了c#連接mdf文件示例,,需要的朋友可以參考下
    2014-03-03
  • 基于C#的圖表控件庫(kù) ScottPlot編譯visual studio 2022

    基于C#的圖表控件庫(kù) ScottPlot編譯visual studio 2022

    基于 C# 的 圖表控件庫(kù) ScottPlot,開源免費(fèi),可以用于開發(fā)一些上位機(jī)軟件,如電壓、電流波形的顯示,開發(fā)【示波器】圖形界面,可以顯示一些圖表、波形,總之功能比較的強(qiáng)大,本文介紹了基于C#的圖表控件庫(kù) ScottPlot編譯visual studio 2022,需要的朋友可以參考下
    2022-06-06
  • C#給多線程傳參的幾種方式小結(jié)

    C#給多線程傳參的幾種方式小結(jié)

    本文詳細(xì)探討了如何在C#中進(jìn)行線程傳參,包括啟動(dòng)線程時(shí)如何將參數(shù)傳遞給線程函數(shù),以及在多線程環(huán)境下正確使用參數(shù)的方法,對(duì)于理解和實(shí)踐C#線程編程具有重要意義,需要的朋友可以參考下
    2024-10-10
  • c#多線程之間的排他鎖的實(shí)現(xiàn)

    c#多線程之間的排他鎖的實(shí)現(xiàn)

    我們很多時(shí)候會(huì)碰到這樣的問(wèn)題,使用多線程刷一個(gè)表的數(shù)據(jù)時(shí)需要多個(gè)線程不能重復(fù)提取數(shù)據(jù),那么這個(gè)時(shí)候就需要使用到線程的排他鎖了,本文就詳細(xì)的介紹一下
    2021-08-08

最新評(píng)論