在动画文本上动画文本闪烁

时间:2012-02-07 20:26:56

标签: wpf animation

我有一个按钮,可以在单击时将更多数据加载到数据网格中。为了表明进度,我将按钮的文本更改为从“更多”更改为“正在加载...”,其中期间是动画的。接下来,我想添加一个“闪烁”效果,以便文本引起你的注意。如果您使用过iPhone / iPod Touch,我会想到锁定屏幕上“滑动解锁”文本的效果。

为此,我将一个中间较浅的渐变色块从左向右移动。因为动画连续循环,所以我使用有效范围之外的偏移来在光渐变停止实际可见的时间之间产生延迟。

我实现了这个,但我可以告诉它,由于某种原因,光渐变不是从文本的左边缘开始。它从“加载”中的“a”开始。我接受了它并且它已经存在了一段时间,但我现在回到它只是为了试图理解为什么。在计算动画时,似乎可能正在使用原始文本的度量,但我认为动画在同一个故事板中应该适用于彼此。这是我的代码:

<UserControl.Resources>
    <local:EmptyBatchNumConverter x:Key="emptyBatchNumConverter" />

    <BeginStoryboard x:Key="bsbLoadingMore" x:Name="bsbLoadingMore">
        <Storyboard x:Name="sbLoadingMore">
            <StringAnimationUsingKeyFrames Storyboard.TargetName="txtBtnMoreText" Storyboard.TargetProperty="Text" Duration="0:0:2" FillBehavior="Stop" RepeatBehavior="Forever">
                <DiscreteStringKeyFrame Value="Loading" KeyTime="0:0:0" />
                <DiscreteStringKeyFrame Value="Loading." KeyTime="0:0:0.5" />
                <DiscreteStringKeyFrame Value="Loading.." KeyTime="0:0:1" />
                <DiscreteStringKeyFrame Value="Loading..." KeyTime="0:0:1.5" />
            </StringAnimationUsingKeyFrames>

            <!--Animate the OffSet of the light gradient stop for a "glint" effect. Using -4.5 to 4.5 to delay the visible effect between repeats (and 
                              control the speed relative to the duration). Using an extra .4 seconds to offset the frequency from the text animation. -->
            <DoubleAnimation Storyboard.TargetName="gs2" Storyboard.TargetProperty="Offset" From="-4.5" To="4.5" Duration="0:0:2.4" RepeatBehavior="Forever" />
        </Storyboard>
    </BeginStoryboard>
</UserControl.Resources>

...

<Button Name="btnMore" Grid.Row="1" Style="{StaticResource OasisGridMoreButton}" Click="btnMore_Click" Visibility="Visible" Height="16">
            <Button.Content>
                <TextBlock Name="txtBtnMoreText" MinWidth="48" Text="More..." /> <!--MinWidth = width of "Loading..."-->
            </Button.Content>
            <Button.Foreground>
                <LinearGradientBrush StartPoint="0.2,0" EndPoint="1,1">
                    <GradientStop x:Name="gs1" Color="Black"  Offset="0"/>
                    <GradientStop x:Name="gs2" Color="Cyan" Offset="-4.5"/>
                    <GradientStop x:Name="gs3" Color="Black" Offset="1" />
                </LinearGradientBrush>
            </Button.Foreground>
        </Button>

1 个答案:

答案 0 :(得分:0)

问题在于:

<LinearGradientBrush StartPoint="0.2,0" EndPoint="1,1">

将其更改为:

<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">

这是我制作的完整测试应用程序(减慢闪烁以更好地看到它):

<Window
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" mc:Ignorable="d"
x:Class="WpfApplication4.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
    <Storyboard x:Key="OnLoaded1"/>
</Window.Resources>
<Window.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard x:Name="bsbLoadingMore">
                <Storyboard x:Name="sbLoadingMore">
                    <StringAnimationUsingKeyFrames Storyboard.TargetName="txtBtnMoreText" Storyboard.TargetProperty="Text" Duration="0:0:2" FillBehavior="Stop" RepeatBehavior="Forever">
                        <DiscreteStringKeyFrame Value="Loading" KeyTime="0:0:0" />
                        <DiscreteStringKeyFrame Value="Loading." KeyTime="0:0:0.5" />
                        <DiscreteStringKeyFrame Value="Loading.." KeyTime="0:0:1" />
                        <DiscreteStringKeyFrame Value="Loading..." KeyTime="0:0:1.5" />
                    </StringAnimationUsingKeyFrames>

                    <!--Animate the OffSet of the light gradient stop for a "glint" effect. Using -4.5 to 4.5 to delay the visible effect between repeats (and 
                                      control the speed relative to the duration). Using an extra .4 seconds to offset the frequency from the text animation. -->
                    <DoubleAnimation Storyboard.TargetName="gs2" Storyboard.TargetProperty="Offset" From="-4.5" To="4.5" Duration="0:0:5.4" RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>

<Grid x:Name="LayoutRoot">
    <Button x:Name="btnMore"  Visibility="Visible" Margin="0,213,0,182" d:LayoutOverrides="GridBox">
        <Button.Foreground>
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                <GradientStop x:Name="gs1" Color="Black"  Offset="0"/>
                <GradientStop x:Name="gs2" Color="Cyan" Offset="0"/>
                <GradientStop x:Name="gs3" Color="Black" Offset="1" />
            </LinearGradientBrush>
        </Button.Foreground>
            <TextBlock x:Name="txtBtnMoreText" MinWidth="48" Text="More..." />
    </Button>
</Grid>

出于某种原因,它最终没有显示</Window> ...