WPF中的基本动画

1. 使用代码创建动画

private void Button_Click(object sender, RoutedEventArgs e)
{
    DoubleAnimation withAnimation=new DoubleAnimation();
    withAnimation.To = this.Width - 30;
    withAnimation.From = Button1.ActualWidth;
    withAnimation.Duration = TimeSpan.FromSeconds(1);
    Button1.BeginAnimation(Button.WidthProperty,withAnimation);

    DoubleAnimation heightAnimation=new DoubleAnimation();
    heightAnimation.To = Height - 30;
    heightAnimation.From = Button1.ActualHeight;
    heightAnimation.Duration = TimeSpan.FromSeconds(1);
    Button1.BeginAnimation(Button.HeightProperty,heightAnimation);
}
<Grid>
    <Button x:Name="Button1" Margin="5" Height="25" HorizontalAlignment="Center" VerticalAlignment="Top" Click="Button_Click">Generate</Button>
</Grid>

2. 动画的生命周期

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation withAnimation=new DoubleAnimation();
            withAnimation.To = this.Width - 30;
            withAnimation.From = Button1.ActualWidth;
            withAnimation.Duration = TimeSpan.FromSeconds(1);
            withAnimation.AutoReverse = true;
            Button1.BeginAnimation(Button.WidthProperty,withAnimation);

            DoubleAnimation heightAnimation=new DoubleAnimation();
            heightAnimation.To = Height - 30;
            heightAnimation.From = Button1.ActualHeight;
            heightAnimation.Duration = TimeSpan.FromSeconds(1);
            //heightAnimation.FillBehavior = FillBehavior.Stop;
            Button1.BeginAnimation(Button.HeightProperty,heightAnimation);
            heightAnimation.Completed += animationComleted;
        }

        private void animationComleted(object sender, EventArgs e)
        {
            Button1.BeginAnimation(Button.WidthProperty,null);
            Button1.Height = 45;
        }

属性

AccelerationRatio 获取或设置一个值,该值指定在将时间消逝从零加速到其最大速率的过程中所占用时间线的 Duration 的百分比。(Inherited from Timeline)
AutoReverse 获取或设置一个值,该值指示时间线在完成向前迭代后是否按相反的顺序播放。(Inherited from Timeline)
BeginTime 获取或设置此 Timeline 应开始的时间。(Inherited from Timeline)
By 获取或设置动画更改其起始值所依据的总数。
CanFreeze 获取一个值,该值指示是否可将对象变为不可修改。(Inherited from Freezable)
DecelerationRatio 获取或设置一个值,该值指定在将时间消逝从其最大速率减速到零的过程中所占用时间线的 Duration 的百分比。(Inherited from Timeline)
DependencyObjectType 获取对此实例的 CLR 类型进行包装的 DependencyObjectType。(Inherited from DependencyObject)
Dispatcher 获取与此 Dispatcher 关联的 DispatcherObject。(Inherited from DispatcherObject)
Duration 获取或设置此时间线播放的时间长度,而不是计数重复。(Inherited from Timeline)
EasingFunction 获取或设置应用于此动画的缓动函数。
FillBehavior 获取或设置一个值,该值指定 Timeline 在到达其有效期末尾后的行为。(Inherited from Timeline)
From 获取或设置动画的起始值。
HasAnimatedProperties 获取一个值,该值指示一个或多个 AnimationClock 对象是否与此对象的任何依赖项属性相关联。(Inherited from Animatable)
IsAdditive 获取或设置一个值,该值指示是否应将目标属性的当前值添加到此动画的起始值。
IsCumulative 获取或设置一个值,该值指定动画重复时是否累计该动画的值。
IsDestinationDefault 获取一个值,该值指示此动画是否将 GetCurrentValue(Object, Object, AnimationClock) 方法的 defaultDestinationValue 参数用作其目标值。(Inherited from AnimationTimeline)
IsFrozen 获取一个值,该值指示对象当前是否可修改。(Inherited from Freezable)
IsSealed 获取一个值,该值指示此实例当前是否为密封的(只读)。(Inherited from DependencyObject)
Name 获取或设置此 Timeline 的名称。(Inherited from Timeline)
RepeatBehavior 获取或设置此时间线的重复行为。(Inherited from Timeline)
SpeedRatio 获取或设置此 Timeline 的时间相对于其父级的前进速率。(Inherited from Timeline)
TargetPropertyType 获取此动画生成的值的类型。(Inherited from DoubleAnimationBase)
To 获取或设置动画的结束值。
private void Button_Click(object sender, RoutedEventArgs e)
{
    DoubleAnimation withAnimation=new DoubleAnimation();
    withAnimation.To = this.Width - 30;
    withAnimation.From = Button1.ActualWidth;
    withAnimation.Duration = TimeSpan.FromSeconds(1);
    withAnimation.AutoReverse = true;
    Button1.BeginAnimation(Button.WidthProperty,withAnimation);

    DoubleAnimation heightAnimation=new DoubleAnimation();
    heightAnimation.To = Height - 30;
    heightAnimation.From = Button1.ActualHeight;
    heightAnimation.Duration = TimeSpan.FromSeconds(1);
    //heightAnimation.FillBehavior = FillBehavior.Stop;
    Button1.BeginAnimation(Button.HeightProperty,heightAnimation);
    heightAnimation.Completed += animationComleted;
}

private void animationComleted(object sender, EventArgs e)
{
    Button1.BeginAnimation(Button.WidthProperty,null);
    Button1.Height = 45;
}