WPF 跳动的文字

时间:2022-05-03
本文章向大家介绍WPF 跳动的文字,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

先上代码

  public MainWindow()
        {
            InitializeComponent();
            //跳动的文字
           Storyboard perChar = new Storyboard();
            _text.TextEffects = new TextEffectCollection();
            for (int i = 0; i < _text.Text.Length; i++)
            {
                TextEffect effect = new TextEffect();
                effect.Transform = new TranslateTransform();
                effect.PositionStart = i;
                effect.PositionCount = 1;
                _text.TextEffects.Add(effect);

                DoubleAnimation anim = new DoubleAnimation();
                anim.To = 25;
                anim.AccelerationRatio = .2;
                anim.DecelerationRatio = .2;
                anim.RepeatBehavior = RepeatBehavior.Forever;
                anim.AutoReverse = true;
                anim.Duration = TimeSpan.FromSeconds(2);
                anim.BeginTime = TimeSpan.FromMilliseconds(250 * i);
                Storyboard.SetTargetProperty(anim, new PropertyPath("TextEffects[" + i + "].Transform.Y"));
                Storyboard.SetTargetName(anim, _text.Name);

                perChar.Children.Add(anim);
            }
            perChar.Begin(this);
     
        }

效果图如下:

前台代码如下:

  <TextBlock FontSize="36pt" Name="_text" Grid.Row="1" Grid.ColumnSpan="3" VerticalAlignment="Center" >
            This is animated Text
        </TextBlock>

 我是初学者,如有什么错误多多指教。也是为了留个印记说不定以后用的着。