14WPF---关键帧动画

1.动画类型

Linear+类型 +KeyGrame:线性变化关键帧,简单线性动画的处理基本一样

DiscreteDoubleKeyFrame:离散变化关键帧,不连续变化

SplineDoubleKeyFrame:样条关键帧,样条函数(二次贝塞尔曲线)

EasingDoubleKeyFrame:缓冲式关键帧

ObjectAnimationUsingKeyFrame:理论上让任意类型参与动画

2.线性和离散变化关键帧

<Window x:Class="WPF___关键帧动画.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:WPF___关键帧动画"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Storyboard x:Key="sb">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="bor"
                                           Storyboard.TargetProperty="Width">
                <!--细化控制-->
                <!--LinearDoubleKeyFrame与DiscreteDoubleKeyFrame的区别是,前者是缓慢的变化
                过程,后者是到达KeyTime,突然变化到Value-->
                <LinearDoubleKeyFrame KeyTime="0:0:1" Value="200"/>
                <LinearDoubleKeyFrame KeyTime="0:0:2" Value="150"/>
                <DiscreteDoubleKeyFrame KeyTime="0:0:4" Value="400"/>
                <LinearDoubleKeyFrame KeyTime="0:0:5" Value="350"/>
                <DiscreteDoubleKeyFrame KeyTime="0:0:8" Value="400"/>
            </DoubleAnimationUsingKeyFrames>
            <!--离散-->
            <StringAnimationUsingKeyFrames Storyboard.TargetName="txt"
                                           Storyboard.TargetProperty="Text">
                <DiscreteStringKeyFrame KeyTime="0:0:0" Value=""/>
                <DiscreteStringKeyFrame KeyTime="0:0:0.5" Value="H"/>
                <DiscreteStringKeyFrame KeyTime="0:0:1" Value="He"/>
                <DiscreteStringKeyFrame KeyTime="0:0:1.5" Value="Hel"/>
                <DiscreteStringKeyFrame KeyTime="0:0:2" Value="Hell"/>
                <DiscreteStringKeyFrame KeyTime="0:0:2.5" Value="Hello"/>
            </StringAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn">
            <BeginStoryboard Storyboard="{StaticResource sb}"/>
        </EventTrigger>
    </Window.Triggers>
    <Grid>
        <StackPanel>
            <TextBlock x:Name="txt"/>
            <Border x:Name="bor" Height="30" Width="100" Background="OrangeRed"/>
        </StackPanel>
        <Button Content="执行动画" Height="30" VerticalAlignment="Bottom" Name="btn"/>
    </Grid>
</Window>

3.SplineDoubleKeyFrame关键帧

<Window x:Class="WPF___关键帧动画.SplineDoubleKeyFrame"
        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:WPF___关键帧动画"
        mc:Ignorable="d"
        Title="SplineDoubleKeyFrame" Height="450" Width="800">
    <Window.Resources>
        <Storyboard x:Key="sb" RepeatBehavior="Forever">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="tt1"
                                           Storyboard.TargetProperty="X">
                <SplineDoubleKeyFrame KeyTime="0:0:1" Value="300" KeySpline="0.1,0.6,0.9,0.4"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn">
            <BeginStoryboard Storyboard="{StaticResource sb}"/>
        </EventTrigger>
    </Window.Triggers>
    <Grid>
        <StackPanel>
            <Grid Width="300" Height="30" Background="Orange">
                <Ellipse Width="10" Height="10" Fill="White" HorizontalAlignment="Left">
                    <Ellipse.RenderTransform>
                        <TranslateTransform X="-10" x:Name="tt1"/>
                    </Ellipse.RenderTransform>
                </Ellipse>
            </Grid>
        </StackPanel>
        <Button Content="执行动画" VerticalAlignment="Bottom" Height="30" Name="btn"/>
    </Grid>
</Window>

KeySpline="0.1,0.6,0.9,0.4"说明:动画的变换速度曲线

 4.EasingDoubleKeyFrame缓冲式关键帧

动画效果

    <Window.Resources>
        <Storyboard x:Key="sb_1">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_1"
                             Storyboard.TargetProperty="Width">
                <!--EasingMode:缓冲模式-->
                <!--Oscillations:速度-->
                <!--Springiness:力度-->
                <DoubleAnimation.EasingFunction>
                    <ElasticEase Oscillations="3" Springiness="3" EasingMode="EaseIn"/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="sb_2">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_2"
                             Storyboard.TargetProperty="Width">
                <!--Bounces:弹跳次数-->
                <!--Bounciness:弹跳力度-->
                <DoubleAnimation.EasingFunction>
                    <BounceEase Bounces="3" Bounciness="2"/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="sb_3">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_3"
                             Storyboard.TargetProperty="Width">
                <!--Amplitude:收缩幅度-->
                <DoubleAnimation.EasingFunction>
                    <BackEase Amplitude="1" />
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="sb_4">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_4"
                             Storyboard.TargetProperty="Width">
                <DoubleAnimation.EasingFunction>
                    <CircleEase/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="sb_5">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_5"
                             Storyboard.TargetProperty="Width">
                <DoubleAnimation.EasingFunction>
                    <PowerEase/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="sb_6">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_6"
                             Storyboard.TargetProperty="Width">
                <!--Exponent:插值的指数-->
                <DoubleAnimation.EasingFunction>
                    <ExponentialEase Exponent="1"/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="sb_7">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_7"
                             Storyboard.TargetProperty="Width">
                <DoubleAnimation.EasingFunction>
                    <QuadraticEase/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="sb_8">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_8"
                             Storyboard.TargetProperty="Width">
                <DoubleAnimation.EasingFunction>
                    <SineEase/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="sb_9">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_9"
                             Storyboard.TargetProperty="Width">
            </DoubleAnimation>
        </Storyboard>
        <!--
        SpeedRatio="0.3"
        AccelerationRatio="0.1"
        DecelerationRatio="0.1"
                    FillBehavior="HoldEnd"
        
        IsAdditive="True"
        RepeatBehavior="Forever"
        IsCumulative="True"
        -->

        <Style TargetType="Button">
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="Width" Value="120"/>
        </Style>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn_1">
            <BeginStoryboard Storyboard="{StaticResource sb_1}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn_2">
            <BeginStoryboard Storyboard="{StaticResource sb_2}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn_3">
            <BeginStoryboard Storyboard="{StaticResource sb_3}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn_4">
            <BeginStoryboard Storyboard="{StaticResource sb_4}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn_5">
            <BeginStoryboard Storyboard="{StaticResource sb_5}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn_6">
            <BeginStoryboard Storyboard="{StaticResource sb_6}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn_7">
            <BeginStoryboard Storyboard="{StaticResource sb_7}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn_8">
            <BeginStoryboard Storyboard="{StaticResource sb_8}"/>
        </EventTrigger>

        <EventTrigger RoutedEvent="Button.Click" SourceName="btn_9">
            <BeginStoryboard Storyboard="{StaticResource sb_9}"/>
        </EventTrigger>
    </Window.Triggers>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border Background="Orange" Width="50" Height="30" Name="bor_1" Grid.Row="0"/>
        <Button Content="ElasticEase" Name="btn_1"/>
        <Border Background="Red" Width="50" Height="30" Name="bor_2" Grid.Row="1"/>
        <Button Content="BounceEase" Grid.Row="1" Name="btn_2"/>
        <Border Background="Green" Width="50" Height="30" Name="bor_3" Grid.Row="2"/>
        <Button Content="BackEase" Grid.Row="2" Name="btn_3"/>
        <Border Background="Blue" Width="50" Height="30" Name="bor_4" Grid.Row="3"/>
        <Button Content="CircleEase" Grid.Row="3" Name="btn_4"/>
        <Border Background="Orange" Width="50" Height="30" Name="bor_5" Grid.Row="4"/>
        <Button Content="PowerEase" Grid.Row="4" Name="btn_5"/>
        <Border Background="Red" Width="50" Height="30" Name="bor_6" Grid.Row="5"/>
        <Button Content="ExponentialEase" Grid.Row="5" Name="btn_6"/>
        <Border Background="Green" Width="50" Height="30" Name="bor_7" Grid.Row="6"/>
        <Button Content="QuadraticEase" Grid.Row="6" Name="btn_7"/>
        <Border Background="Blue" Width="50" Height="30" Name="bor_8" Grid.Row="7"/>
        <Button Content="SineEase" Grid.Row="7" Name="btn_8"/>

        <Border Background="Orange" Grid.Row="9" Width="50" Height="30" Name="bor_9"/>
        <Button Content="扩展属性" Grid.Row="9" Name="btn_9"/>
    </Grid>

5.ObjectAnimationUsingKeyFrame:理论上让任意类型参与动画

   <Window.Resources>
        <Storyboard x:Key="sb">
            <!--闪动,还可以设置透明度Opacity来实现-->
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="txt"
                                           RepeatBehavior="Forever"
                                           Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0:0:1" >
                    <DiscreteObjectKeyFrame.Value>
                       <Visibility>
                            Hidden
                           
                       </Visibility>
                    </DiscreteObjectKeyFrame.Value>

                </DiscreteObjectKeyFrame>
                <DiscreteObjectKeyFrame KeyTime="0:0:2" >
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>
                            Visible
                        </Visibility>
                    </DiscreteObjectKeyFrame.Value>

                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn">
            <BeginStoryboard Storyboard="{StaticResource sb}"/>
        </EventTrigger>
    </Window.Triggers>
    <Grid>
        <StackPanel>
            <TextBlock x:Name="txt" Text="Hello"/>
            <Border x:Name="bor" Height="30" Width="100" Background="OrangeRed"/>
        </StackPanel>
        <Button Content="执行动画" Height="30" VerticalAlignment="Bottom" Name="btn"/>
    </Grid>

6.动画拓展属性

SpeedRatio:播放速度

        <Storyboard x:Key="sb_9" SpeedRatio="3">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_9"
                             Storyboard.TargetProperty="Width">
            </DoubleAnimation>
        </Storyboard>

AccelerationRatio:加速速率

DecelerationRatio:减速速率

        <Storyboard x:Key="sb_9"       
        AccelerationRatio="0.1"
        DecelerationRatio="0.1">
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_9"
                             Storyboard.TargetProperty="Width">
            </DoubleAnimation>
        </Storyboard>

 AccelerationRatio="0.1"  和 DecelerationRatio="0.1":

开始10%加速,最后10%减速,两者相加小于1

AutoReverse:是否执行相反的动画

        <Storyboard x:Key="sb_9" 
                    AutoReverse="True"
        >
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_9"
                             Storyboard.TargetProperty="Width">
            </DoubleAnimation>
        </Storyboard>

FillBehavior:动画结束HoldEnd(保持最后的结果)/Stop

        <Storyboard x:Key="sb_9" 
                   FillBehavior="HoldEnd"
        >
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             Storyboard.TargetName="bor_9"
                             Storyboard.TargetProperty="Width">
            </DoubleAnimation>
        </Storyboard>

RepeatBehavior:动画重复方式,次数/时间/Forever

IsAddtive:将目标属性的当前值添加到动画的起始值

        <Storyboard x:Key="sb_9" 
                   
        >
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             IsAdditive="True"
                             Storyboard.TargetName="bor_9"
                             Storyboard.TargetProperty="Width">
            </DoubleAnimation>
        </Storyboard>

只能在Animation中 

IsCumulative:如果动画不断重复,就积累动画值

        <Storyboard x:Key="sb_9" 
               
        >
            <DoubleAnimation Duration="0:0:2"
                             From="50"
                             To="300"
                             RepeatBehavior="Forever"    
                             IsCumulative="True"
                             Storyboard.TargetName="bor_9"
                             Storyboard.TargetProperty="Width">
            </DoubleAnimation>
        </Storyboard>

只能在Animation中 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值