WPF实现文字粒子闪烁动画效果
本文实例为大家分享了WPF实现文字粒子闪烁动画的具体代码,供大家参考,具体内容如下
实现效果如下:
思路:首先根据显示文本创建文本路径Geometry,然后在路径内随机生成圆形粒子并添加动画。
步骤:
1、粒子类Particle.cs
publicclassParticle { //////形状 /// publicEllipseShape; //////坐标 /// publicPointPosition; }
2、粒子系统ParticleSystem.cs
//////粒子路径 /// privateGeometryparticleGeometry; //////粒子个数 /// privateintparticleCount=100; //////粒子最小尺寸 /// privatestaticintsizeMin=10; //////粒子最大尺寸 /// privateintsizeMax=20; //////随机数 /// privateRandomrandom; //////粒子列表 /// privateListparticles; /// ///粒子容器 /// privateCanvascontainerParticles; publicParticleSystem(Geometry_path,int_maxRadius,int_particleCount,Canvas_containerParticles) { particleGeometry=_path; particleCount=_particleCount; sizeMax=_maxRadius; containerParticles=_containerParticles; random=newRandom(); particles=newList(); SpawnParticle(); } /// ///初始化粒子 /// privatevoidSpawnParticle() { //清空粒子队列 particles.Clear(); containerParticles.Children.Clear(); //生成粒子 for(inti=0;i///设置粒子大小动画 /// privatevoidSetParticleSizeAnimation(Ellipsep) { Storyboardsb=newStoryboard(); //动画完成事件再次设置此动画 sb.Completed+=(S,E)=> { SetParticleSizeAnimation(p); }; intsize=random.Next(sizeMin,sizeMax+1); inttime=random.Next(100,1000); DoubleAnimationdaX=newDoubleAnimation(size,newDuration(TimeSpan.FromMilliseconds(time))); DoubleAnimationdaY=newDoubleAnimation(size,newDuration(TimeSpan.FromMilliseconds(time))); Storyboard.SetTarget(daX,p); Storyboard.SetTarget(daY,p); Storyboard.SetTargetProperty(daX,newPropertyPath("Width")); Storyboard.SetTargetProperty(daY,newPropertyPath("Height")); sb.Children.Add(daX); sb.Children.Add(daY); sb.Begin(); } /// ///获取随机颜色画刷 /// privateSolidColorBrushGetRandomColorBursh() { byter=(byte)random.Next(128,256); byteg=(byte)random.Next(128,256); byteb=(byte)random.Next(128,256); returnnewSolidColorBrush(Color.FromArgb(125,r,g,b)); }
3、主窗体交互
privateParticleSystemps; publicMainWindow() { InitializeComponent(); this.Loaded+=MainWindow_Loaded; } privatevoidMainWindow_Loaded(objectsender,RoutedEventArgse) { Geometryg=CreateTextPath("HELLO",newPoint(this.cvs_particleContainer.Margin.Left,this.cvs_particleContainer.Margin.Top),newTypeface(newFontFamily("Arial"),FontStyles.Normal,FontWeights.Bold,FontStretches.Normal),200); ps=newParticleSystem(g,25,350,this.cvs_particleContainer); } //////创建文本路径 /// ///文本字符串 /// 显示位置 /// 字体信息 /// 字体大小 /// privateGeometryCreateTextPath(stringword,Pointpoint,Typefacetypeface,intfontSize) { FormattedTexttext=newFormattedText(word,newSystem.Globalization.CultureInfo("en-US"),FlowDirection.LeftToRight,typeface,fontSize,Brushes.Black); Geometryg=text.BuildGeometry(point); PathGeometrypath=g.GetFlattenedPathGeometry(); returnpath; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。