天天看點

WPF特效:流光動畫效果

今天有人問我想實作一個流光效果。我簡單的錄了一下他想實作效果的GIF。

WPF特效:流光動畫效果
實際效果如下:因為今天有事情,隻是大概實作了一個效果。主要使用PointAnimationUsingKeyFrames動畫效果來實作。以後有空了會寫一些更好看的。
WPF特效:流光動畫效果
 代碼如下:

<Window x:Class="StreamerEffect.MainWindow"
        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:StreamerEffect"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
   
        <Storyboard x:Key="Storyboard1" RepeatBehavior="Forever">
            <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(LinearGradientBrush.StartPoint)" 
                                          Storyboard.TargetName="rectangle">
                <EasingPointKeyFrame KeyTime="0:0:0.5" Value="0.855,0.148"/>
                <EasingPointKeyFrame KeyTime="0:0:1" Value="0.852,0.855"/>
                <EasingPointKeyFrame KeyTime="0:0:1.5" Value="0.148,0.855"/>
                <EasingPointKeyFrame KeyTime="0:0:2" Value="0.144,0.149"/>
                <EasingPointKeyFrame KeyTime="0:0:2.5" Value="0,0"/>

            </PointAnimationUsingKeyFrames>
            <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(LinearGradientBrush.EndPoint)" 
                                          Storyboard.TargetName="rectangle">
                <EasingPointKeyFrame KeyTime="0:0:0.5" Value="0.145,0.852"/>
                <EasingPointKeyFrame KeyTime="0:0:1" Value="0.148,0.145"/>
                <EasingPointKeyFrame KeyTime="0:0:1.5" Value="0.852,0.145"/>
                <EasingPointKeyFrame KeyTime="0:0:2" Value="0.856,0.851"/>
                <EasingPointKeyFrame KeyTime="0:0:2.5" Value="0,1"/>

            </PointAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
        </EventTrigger>  
    </Window.Triggers>
    <Grid> 
        <Rectangle x:Name="rectangle" Width="100" Height="100" 
                   StrokeThickness="3">
            <Rectangle.Stroke>
                <LinearGradientBrush  Opacity="0.6">
                    <GradientStop Color="Transparent" Offset="0"/>
                    <GradientStop Color="Violet"  Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Stroke>

        </Rectangle>
    </Grid>
</Window>