WPF 动画 插值动画、关键帧动画、路径动画

WPF动画,分为三种:插值动画、关键帧动画、路径动画

2.1 插值动画:

1)定义:插值动画是指,属性值从某一个值,经过一段时间后,连续变化值另一个值的动画。

例如宽度属性,类型为double,可以设定动画为在1s的时间内,值从0变到10。此时WPF内部会采用插值算法,填充1与10之间的值。

2)命名规则:数据类型+Animation。例如double类型动画:DoubleAnimation

2.2 关键帧动画:

1)定义:对于属性类型为离散量类型的,因为无法进行插值运算。因此只能填充"帧"。

例如Name属性,类型是string。当属性值从"raymond" 变到 "jack"的时候,是无法进行插值运算的。此时就只能用关键帧来代替。在某一个时候,显示"raymond",然后在下一个时候,显示"jack"。

2)命名规则:数据类型+AnimationUsingKeyFrames。例如double类型:DoubleAnimationUsingKeyFrames

2.3 路径动画:

1)路径动画,是指让某个元素用来沿着路径的方向进行变换的动画。

2)命名规则:数据类型+AnimationUsingPath。例如double类型DoubleAnimationUsingPath

Storyboard 是动画的容器

  1. BeginStoryboard 开始播放
  2. PauseStoryboard 暂停播放
  3. StopStoryboard 停止播放
  4. ResumeStoryboard 恢复播放、继续播放

DoubleAnimation

》》 xmal方式

csharp 复制代码
<Button Name="btn" Content="Click Me" HorizontalAlignment="Left" VerticalAlignment="Top">
    <Button.Triggers>
        <EventTrigger RoutedEvent="{x:Static Button.MouseEnterEvent}">
            <BeginStoryboard Name="sizeStoryboard">
                <Storyboard Storyboard.TargetName="btn" >
                    <DoubleAnimation From="{Binding Path=ActualWidth,ElementName=btn}" To="100" Duration="0:0:1" Storyboard.TargetName="btn" Storyboard.TargetProperty="Width"/>
                    <DoubleAnimation From="{Binding Path=ActualHeight,ElementName=btn}" To="50" Duration="0:0:1" Storyboard.TargetName="btn" Storyboard.TargetProperty="Height"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
		<!--下面这个触发器,只是为了演示效果,把停止、暂停、继续  放在一起啦-->
		 <EventTrigger RoutedEvent="{x:Static Button.MouseLeaveEvent}">
		     <!--停止Storyboard:属性值会回到原始状态-->
		     <StopStoryboard BeginStoryboardName="sizeStoryboard" />
		     <!--暂停Storyboard-->
		     <PauseStoryboard BeginStoryboardName="sizeStoryboard"/>
		     <!--继续Storyboard:继续已经暂定的Storyboard-->
		     <ResumeStoryboard BeginStoryboardName="sizeStoryboard"/>
		 </EventTrigger>
    </Button.Triggers>
</Button>

如果多个动画都是应用同一个元素,同一个属性,可以将Target、TargetName、TargetProperty等附加属性写在Storyboard中。

》》》code

》》》或者

csharp 复制代码
Private void DemoButton_Click(object sender,RoutedEventArgs e)
{
    DoubleAnimation dAnimation = new DoubleAnimation();
    dAnimation.From = 100;
    dAnimation.To = 300;
    dAnimation.Duration = new Duration(TimeSpan.FromSeconds(2));
    btn.BeginAnimation(Button.WidthProperty,dAnimation);
}

这里需要说明几点:

1)Animation不能直接在xaml中写。如果要在xaml中写Animation,则需要结合Storyboard。

2)要应用动画,需要调用元素的BeginAnimation()方法。需要注意,BeginAnimation()方法由IAnimatable接口定义(只要实现了该接 口,都会有这个方法),但只有元素调用这个方法才能应用动画。如果是Animation类的实例调用该方法,没有意义,因为没有指定动画应用于哪个对象。

资源方式 动画
相关推荐
大道随心7 分钟前
【WPF】02 按钮控件圆角配置及状态切换
wpf
jyl_sh6 小时前
Ribbon布局和尺寸调整
ribbon·c#·wpf·客户端
△曉風殘月〆1 天前
在WPF中保存控件内容为图片
wpf
芝麻科技1 天前
Wpf使用NLog将日志输出到LogViewer
wpf·prism
△曉風殘月〆2 天前
WPF颜色(SolidColorBrush)和Win32颜色(COLOREF)互转的方法
wpf·win32·solidcolorbrush·colorref
shanshan20992 天前
上位机系统架构 | 如何设计一个高效的多相机管理系统
c#·wpf·相机
充值内卷2 天前
WPF入门教学四 WPF控件概述
windows·ui·wpf
小白3 天前
WPF自定义Dialog模板,内容用不同的Page填充
wpf
Crazy Struggle3 天前
C# + WPF 音频播放器 界面优雅,体验良好
c#·wpf·音频播放器·本地播放器
James.TCG4 天前
WPF动画
wpf