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类的实例调用该方法,没有意义,因为没有指定动画应用于哪个对象。

资源方式 动画
相关推荐
wangnaisheng1 天前
【WPF】Opacity 属性的使用
wpf
姬激薄1 天前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)1 天前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心2 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo2 天前
9.1.领域驱动设计
wpf
大道随心2 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠3 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go3 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet3 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf
冰茶_3 天前
WPF之绑定模式深入
学习·microsoft·微软·c#·wpf·绑定模式