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>

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

posted @ 2013-05-06 14:16  幕三少  阅读(1066)  评论(1编辑  收藏  举报