使用xaml或C#创建光动画

时间:2015-12-11 09:20:32

标签: c# xaml animation

我想动画文字高亮显示像"滑动解锁"苹果手机。 请你帮助我好吗? 谢谢!

iPhone slide

1 个答案:

答案 0 :(得分:3)

这样做的一种方法是使用 LinearGradientBrush ,然后为其渐变停止设置动画以创建“频闪”效果。

以下是基本窗口的XAML,其中一些文字说明了该技术:

<Window x:Class="StackOverflow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Background="Black">
    <TextBlock x:Name="_text" Text="Hello World"
        FontSize="60" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock.Foreground>
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,0"
                ColorInterpolationMode="ScRgbLinearInterpolation">
                <GradientStop Color="#ff666666" Offset="-0.2" />
                <GradientStop Color="#ffffffff" Offset="-0.1" />
                <GradientStop Color="#ff666666" Offset="0" />
            </LinearGradientBrush>
        </TextBlock.Foreground>
        <TextBlock.Triggers>
            <EventTrigger>
                <BeginStoryboard>
                    <Storyboard RepeatBehavior="Forever" EnableDependentAnimation="True">
                        <DoubleAnimation Storyboard.TargetName="_text"
                            Storyboard.TargetProperty="(TextBlock.Foreground).(GradientBrush.GradientStops)[0].(GradientStop.Offset)"
                            From="-0.2" To="1.5" Duration="0:0:1.5" />
                        <DoubleAnimation Storyboard.TargetName="_text"
                            Storyboard.TargetProperty="(TextBlock.Foreground).(GradientBrush.GradientStops)[1].(GradientStop.Offset)"
                            From="-0.1" To="1.6" Duration="0:0:1.5" />
                        <DoubleAnimation Storyboard.TargetName="_text"
                            Storyboard.TargetProperty="(TextBlock.Foreground).(GradientBrush.GradientStops)[2].(GradientStop.Offset)"
                            From="0" To="1.7" Duration="0:0:1.5" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </TextBlock.Triggers>
    </TextBlock>
</Window>

您可以尝试 GradientStop 颜色和偏移,再加上 DoubleAnimation 元素的持续时间,以获得您所追求的效果。请记住,所有三个动画元素的持续时间必须相同。