这是我的代码:
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Padding="10" Name="cmdGrow" Click="cmdGrow_Click" Height="40" Width="160"
HorizontalAlignment="Center" VerticalAlignment="Center" Content="Grow Button #1 "></Button>
<Button Grid.Row="1" Padding="10" Name="cmdShrink" Click="cmdShrink_Click"
HorizontalAlignment="Center" VerticalAlignment="Center" Content="Shrink Button #1"></Button>
</Grid>
后台代码:
namespace Animation
{
public partial class CodeAnimation : System.Windows.Window
{
public CodeAnimation()
{
InitializeComponent();
}
private void cmdGrow_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation widthAnimation = new DoubleAnimation();
widthAnimation.Duration = TimeSpan.FromSeconds(5);
widthAnimation.To = 270;
cmdGrow.BeginAnimation(Button.WidthProperty, widthAnimation);
}
private void cmdShrink_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation widthAnimation = new DoubleAnimation();
widthAnimation.Duration = TimeSpan.FromSeconds(5);
//widthAnimation.To = 20;
cmdGrow.BeginAnimation(Button.WidthProperty, widthAnimation);
}
}
}
产生以下简单的UI:
在cmdShrink_Click()事件中,如果未指定动画的“ To”属性,则反向动画(即“收缩”)比向前动画(即“增长”)要慢得多。我已经通过在cmdShrink_click()事件中放置一个断点来检查Duration属性,它显示为5秒:
要进行复制,请单击“增长”按钮,而不是在“增长”动画完成之前半按“收缩”按钮。
好像多个动画的持续时间具有累积效果。
我在Windows 7,.NET FW 4客户端配置文件,VS 2010上。
动画速度较慢,因为它必须在相同的时间量(5秒)内覆盖较小的大小差异。增长动画的速率基于其预期的最终大小,但是您将动画剪短了。如果您将增长动画停止到一半,则缩小动画需要覆盖的距离是增长动画预期覆盖的一半,但是您仍在告诉它要花整整五秒钟的时间。为了在相同的时间内覆盖一半的距离,它以一半的速度前进。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句