WPF 文字跳动动画

这篇博客展示了如何使用XAML代码创建一个文本效果,使得文字中的每个字符顺序地向上或向下跳动,形成一种动态的视觉效果。通过TextEffect和Storyboard动画实现了文字的连续移动,为UI增加了趣味性。
摘要由CSDN通过智能技术生成

演示:一串文字中单个文字顺序连续跳动 

代码:

<Window x:Class="Test.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test"
         xmlns:MyNamespace="clr-namespace:Test"
        mc:Ignorable="d" Name="win"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        Title="Window1" Height="450" Width="800" Loaded="Window_Loaded">
    <Window.Resources>
        <DrawingBrush x:Key="MyWireBrushResource" 
      Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile">
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <GeometryDrawing Geometry="M0,0 L1,0 1,0.1, 0,0.1Z" Brush="#66CCCCFF" />
                        <GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="#66CCCCFF" />
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Window.Resources>
    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
        <Border x:Name="txtBd" Background="{StaticResource MyWireBrushResource}">
            <TextBlock x:Name="txt" Text="12345678"  FontSize="60"   Margin="40">
                <TextBlock.TextEffects>
                    <TextEffect PositionCount="1" x:Name="MyTextEffect">
                        <TextEffect.Transform>
                            <TransformGroup>
                                <TranslateTransform x:Name="TextEffectTranslateTransform"/>
                            </TransformGroup>
                        </TextEffect.Transform>
                    </TextEffect>
                </TextBlock.TextEffects>
                <TextBlock.Triggers>
                    <EventTrigger RoutedEvent="TextBlock.Loaded">
                        <BeginStoryboard>
                            <Storyboard>
                                <!-- Animates the Y factor of the 
                     TextEffect's TranslateTransform. -->
                                <DoubleAnimation
                  Storyboard.TargetName="TextEffectTranslateTransform"
                  Storyboard.TargetProperty="Y"                  
                  From="0"
                  To="20"
                  Duration="00:00:0.25"
                  RepeatBehavior="Forever"
                  AutoReverse="True" />

                                <!-- Animates the position of the TextEffect. -->
                                <Int32AnimationUsingKeyFrames
                  Storyboard.TargetName="MyTextEffect"
                  Storyboard.TargetProperty="PositionStart"
                  Duration="0:0:4"
                  AutoReverse="True"
                  RepeatBehavior="Forever">
                                    <Int32AnimationUsingKeyFrames.KeyFrames>
                                        <DiscreteInt32KeyFrame Value="0" KeyTime="0:0:0" />
                                        <DiscreteInt32KeyFrame Value="1" KeyTime="0:0:0.5" />
                                        <DiscreteInt32KeyFrame Value="2" KeyTime="0:0:1" />
                                        <DiscreteInt32KeyFrame Value="3" KeyTime="0:0:1.5" />
                                        <DiscreteInt32KeyFrame Value="4" KeyTime="0:0:2" />
                                        <DiscreteInt32KeyFrame Value="5" KeyTime="0:0:2.5" />
                                        <DiscreteInt32KeyFrame Value="6" KeyTime="0:0:3" />
                                        <DiscreteInt32KeyFrame Value="7" KeyTime="0:0:3.5" />
                                        <DiscreteInt32KeyFrame Value="8" KeyTime="0:0:4" />

                                    </Int32AnimationUsingKeyFrames.KeyFrames>
                                </Int32AnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </TextBlock.Triggers>
            </TextBlock>
        </Border>

        <Rectangle Width="{Binding ActualWidth,ElementName=txtBd}" Height="{Binding ActualHeight,ElementName=txtBd}" HorizontalAlignment="Left" >
            <Rectangle.Fill>
                <VisualBrush Visual="{Binding ElementName=txtBd}">
                    <VisualBrush.RelativeTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="1" ScaleY="-1"/>
                            <TranslateTransform Y="1"/>
                        </TransformGroup>
                    </VisualBrush.RelativeTransform>
                </VisualBrush>
            </Rectangle.Fill>
        </Rectangle>
    </StackPanel>
</Window>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无熵~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值