Enseñarle cómo usar WPF para lograr un efecto de animación intermitente de partículas de texto

Este artículo presenta principalmente WPF para realizar el efecto de animación de parpadeo de partículas de texto en detalle. El código de muestra en el artículo es muy detallado y tiene cierto valor de referencia. Los amigos interesados ​​pueden consultarlo.

El ejemplo de este artículo comparte el código específico de WPF para realizar la animación de parpadeo de partículas de texto para su referencia. El contenido específico es el siguiente

El efecto es el siguiente:

Enseñarle cómo usar WPF para lograr un efecto de animación de parpadeo de partículas de texto. Enseñarle cómo usar WPF para lograr un efecto de animación de parpadeo de partículas de texto.

Ideas:

Primero cree una geometría de ruta de texto basada en el texto mostrado, y luego genere partículas circulares al azar en la ruta y agregue animación.

paso:

1. Clase de partículas Particle.cs

Partícula de clase pública 
 { 
    ///

/// Forma /// Forma de elipse pública; /// /// Coordenadas /// Posición del punto público;}

2. ParticleSystem.cs

/// 

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 (),}, Posición = po,}; SetParticleSizeAnimation (p. Forma); partículas.Añadir (p); Canvas.SetLeft (p.Forma, p.Position.X); Canvas.SetTop (p.Shape, p.Position.Y); containerParticles.Children.Add (p. forma); descanso; }}}} /// /// 设置 粒子 大小 动画 /// private void SetParticleSizeAnimation (Elipse 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 (tamaño, nueva Duración (TimeSpan.FromMilliseconds (tiempo))); DoubleAnimation daY = new DoubleAnimation (tamaño, nueva Duración (TimeSpan.FromMilliseconds (tiempo))); Storyboard.SetTarget (daX, p); Storyboard.SetTarget (daY, p); Storyboard.SetTargetProperty (daX, new PropertyPath ("Width")); Storyboard.SetTargetProperty (daY, new PropertyPath ("Altura")); sb.Children.Add (daX); sb.Children.Add (daY); sb.Begin (); } /// /// 获取 随机 颜色 画 刷 /// private SolidColorBrush GetRandomColorBursh () {byte r = (byte) random.Next (128, 256); byte g = (byte) aleatorio. Siguiente (128, 256); byte b = (byte) aleatorio. Siguiente (128, 256); return new SolidColorBrush (Color.FromArgb (125, r, g, b)); } FromMilliseconds (tiempo))); DoubleAnimation daY = new DoubleAnimation (tamaño, nueva Duración (TimeSpan.FromMilliseconds (tiempo))); Storyboard.SetTarget (daX, p); Storyboard.SetTarget (daY, p); Storyboard.SetTargetProperty (daX, new PropertyPath ("Width")); Storyboard.SetTargetProperty (daY, new PropertyPath ("Altura")); sb.Children.Add (daX); sb.Children.Add (daY); sb.Begin (); } /// /// 获取 随机 颜色 画 刷 /// private SolidColorBrush GetRandomColorBursh () {byte r = (byte) random.Next (128, 256); byte g = (byte) aleatorio. Siguiente (128, 256); byte b = (byte) aleatorio. Siguiente (128, 256); return new SolidColorBrush (Color.FromArgb (125, r, g, b)); } FromMilliseconds (tiempo))); DoubleAnimation daY = new DoubleAnimation (tamaño, nueva Duración (TimeSpan.FromMilliseconds (tiempo))); Storyboard.SetTarget (daX, p); Storyboard.SetTarget (daY, p); Storyboard.SetTargetProperty (daX, new PropertyPath ("Width")); Storyboard.SetTargetProperty (daY, new PropertyPath ("Altura")); sb.Children.Add (daX); sb.Children.Add (daY); sb.Begin (); } /// /// 获取 随机 颜色 画 刷 /// private SolidColorBrush GetRandomColorBursh () {byte r = (byte) random.Next (128, 256); byte g = (byte) aleatorio. Siguiente (128, 256); byte b = (byte) aleatorio. Siguiente (128, 256); return new SolidColorBrush (Color.FromArgb (125, r, g, b)); } new PropertyPath ("Ancho")); Storyboard.SetTargetProperty (daY, new PropertyPath ("Altura")); sb.Children.Add (daX); sb.Children.Add (daY); sb.Begin (); } /// /// 获取 随机 颜色 画 刷 /// private SolidColorBrush GetRandomColorBursh () {byte r = (byte) random.Next (128, 256); byte g = (byte) aleatorio. Siguiente (128, 256); byte b = (byte) aleatorio. Siguiente (128, 256); return new SolidColorBrush (Color.FromArgb (125, r, g, b)); } new PropertyPath ("Ancho")); Storyboard.SetTargetProperty (daY, new PropertyPath ("Altura")); sb.Children.Add (daX); sb.Children.Add (daY); sb.Begin (); } /// /// 获取 随机 颜色 画 刷 /// private SolidColorBrush GetRandomColorBursh () {byte r = (byte) random.Next (128, 256); byte g = (byte) aleatorio. Siguiente (128, 256); byte b = (byte) aleatorio. Siguiente (128, 256); return new SolidColorBrush (Color.FromArgb (125, r, g, b)); }

3. Interacción de la forma principal

ParticleSystem privado ps; 
  
Public MainWindow () 
    { 
      InitializeComponent (); 
      this.Loaded + = MainWindow_Loaded; 
    } 
  
    private void MainWindow_Loaded (remitente del objeto, RoutedEventArgs e) 
    { 
      Geometry g = CreateTextPath ("HELL 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); 
    } 
  
    ///

/// Crear ruta de texto /// /// Cadena de texto /// Posición de visualización /// Información de fuente /// Tamaño de fuente /// Geometría privada CreateTextPath (palabra de cadena, punto, tipo de letra, int fontSize) { FormattedText text = new FormattedText (palabra, nuevo System.Globalization.CultureInfo ("en-US"), FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black); Geometría g = text.BuildGeometry (punto); PathGeometry ruta = g. GetFlattenedPathGeometry (); ruta de retorno;}

Lo anterior es todo el contenido de este artículo, espero que sea de utilidad para el estudio de todos.

La dirección de este artículo: https://www.linuxprobe.com/wpf-linux-six.html

Supongo que te gusta

Origin blog.csdn.net/u014389734/article/details/108539338
Recomendado
Clasificación