新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
通過(guò)WPF實(shí)現(xiàn)文字粒子閃爍
本篇文章重點(diǎn)為大家講解一下通過(guò)WPF實(shí)現(xiàn)文字粒子閃爍具體方法,有需要的小伙伴可以參考一下。

成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的西城網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
實(shí)現(xiàn)效果如下:
思路:
首先根據(jù)顯示文本創(chuàng)建文本路徑Geometry,然后在路徑內(nèi)隨機(jī)生成圓形粒子并添加動(dòng)畫。
步驟:
1、粒子類Particle.cs
public class Particle
{
///
/// 形狀
///
public Ellipse Shape;
///
/// 坐標(biāo)
///
public Point Position;
}
2、粒子系統(tǒng)ParticleSystem.cs
///
/// 粒子路徑
///
private Geometry particleGeometry;
///
/// 粒子個(gè)數(shù)
///
private int particleCount = 100;
///
/// 粒子最小尺寸
///
private static int sizeMin = 10;
///
/// 粒子最大尺寸
///
private int sizeMax = 20;
///
/// 隨機(jī)數(shù)
///
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()
{
//清空粒子隊(duì)列
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;
}
}
}
}
///
/// 設(shè)置粒子大小動(dòng)畫
///
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();
}
///
/// 獲取隨機(jī)顏色畫刷
///
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;
}
網(wǎng)頁(yè)標(biāo)題:通過(guò)WPF實(shí)現(xiàn)文字粒子閃爍
鏈接URL:http://fisionsoft.com.cn/article/cosipeg.html


咨詢
建站咨詢
