WPF實(shí)現(xiàn)文字粒子閃爍動(dòng)畫效果
本文實(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í)有所幫助,也希望大家多多支持腳本之家。
- WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效
- WPF實(shí)現(xiàn)時(shí)鐘特效
- WPF實(shí)現(xiàn)文本描邊+外發(fā)光效果的示例代碼
- WPF實(shí)現(xiàn)動(dòng)畫效果
- WPF實(shí)現(xiàn)3D粒子波浪效果
- WPF實(shí)現(xiàn)平面三角形3D運(yùn)動(dòng)效果
- WPF實(shí)現(xiàn)3D立方體波浪墻效果
- WPF實(shí)現(xiàn)進(jìn)度條實(shí)時(shí)更新效果
- WPF實(shí)現(xiàn)轉(zhuǎn)圈進(jìn)度條效果
- WPF實(shí)現(xiàn)流光動(dòng)畫特效
相關(guān)文章
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-11C#拆分字符串正則表達(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-04C#實(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-04C#實(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#版...2007-08-08Winform自定義控件在界面拖動(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#的圖表控件庫(kù) ScottPlot編譯visual studio 2022
基于 C# 的 圖表控件庫(kù) ScottPlot,開源免費(fèi),可以用于開發(fā)一些上位機(jī)軟件,如電壓、電流波形的顯示,開發(fā)【示波器】圖形界面,可以顯示一些圖表、波形,總之功能比較的強(qiáng)大,本文介紹了基于C#的圖表控件庫(kù) ScottPlot編譯visual studio 2022,需要的朋友可以參考下2022-06-06