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;
}
属性
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;
}
