Teach you how to use WPF to achieve text particle flashing animation effect

This article mainly introduces WPF to achieve the effect of text particle blinking animation. The sample code in the article is very detailed and has certain reference value. Interested friends can refer to it.

The example in this article shares the specific code of WPF to realize the text particle blinking animation for your reference. The specific content is as follows

The effect is as follows:

Teach you how to use WPF to achieve text particle flashing animation effect teach you how to use WPF to achieve text particle flashing animation effect

Ideas:

First create a text path Geometry based on the displayed text, and then randomly generate circular particles in the path and add animation.

step:

1. Particle class Particle.cs

public class Particle
 {
    /// 

/// Shape/// public Ellipse Shape; /// /// Coordinates/// public Point Position;}

2. ParticleSystem.cs

/// 

/// Particle path/// private Geometry particleGeometry; /// /// Number of particles/// private int particleCount = 100; /// /// Minimum particle size/// private static int sizeMin = 10; / // /// Maximum particle size/// private int sizeMax = 20; /// /// random number/// private Random random; /// /// particle list/// private List particles; /// /// particle container/// 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();} /// /// Initialize the particles /// private void SpawnParticle() {//Clear the particle queue particles.Clear(); containerParticles.Children.Clear(); / /Generate particles 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; } } } } /// /// 设置粒子大小动画 /// 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)); }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)); }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)); }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)); }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. Main form interaction

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);
    }
  
    /// 

/// Create text path/// /// Text string/// Display position/// Font information/// Font size/// 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;}

The above is the whole content of this article, I hope it will be helpful to everyone's study.

This article address: https://www.linuxprobe.com/wpf-linux-six.html

Guess you like

Origin blog.csdn.net/u014389734/article/details/108539338
Recommended