WPF实现动画的方式:

  1. 基于计时器的动画

建立一个定时器,然后根据其频率循环调用函数或者一个事件处理函数,在这个函数中可以手工更新目标属性,直到达到最终值,这时可以停止计时器。

案例:

效果图:

XAML:

<Window x:Class="WpfDispatcherTimerAnimation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfDispatcherTimerAnimation"
mc:Ignorable="d"
Title="MainWindow" Height="" Width="">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Ellipse Name="rectangle" Height="" Width="" Fill="Aquamarine"/>
<Button Content="开启动画" Click="ButtonBase_OnClick" Height="" Width="" Grid.Row=""/>
</Grid>
</Window>

C#代码:

using System;
using System.Windows;
using System.Windows.Threading; namespace WpfDispatcherTimerAnimation
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// 长方形减小计时器
/// </summary>
DispatcherTimer dispatcherTimerDecrease = new DispatcherTimer(); /// <summary>
/// 长方形增大计时器
/// </summary>
DispatcherTimer dispatcherTimerIncrease = new DispatcherTimer(); /// <summary>
/// 按钮点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
dispatcherTimerIncrease.Interval = TimeSpan.FromMilliseconds();
dispatcherTimerIncrease.Tick += dispatcherTimerIncrease_Tick;
dispatcherTimerIncrease.Start();
dispatcherTimerDecrease.Interval = TimeSpan.FromMilliseconds();
dispatcherTimerDecrease.Tick += DispatcherTimerDecrease_Tick;
} /// <summary>
/// 增加计时器事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DispatcherTimerDecrease_Tick(object sender, EventArgs e)
{
if (rectangle.Width < || rectangle.Height < )
{
(sender as DispatcherTimer).Stop();
dispatcherTimerIncrease.Start();
}
else if (rectangle.Width >= || rectangle.Height >= )
{
rectangle.Width -= ;
rectangle.Height -= ;
}
} /// <summary>
/// 减少计时器事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dispatcherTimerIncrease_Tick(object sender, EventArgs e)
{
if (rectangle.Width < || rectangle.Height < )
{
rectangle.Width += ;
rectangle.Height += ;
}
else if (rectangle.Width >= || rectangle.Height >= )
{
(sender as DispatcherTimer).Stop();
dispatcherTimerDecrease.Start();
}
}
}
}

2.基于桢的动画

由CompositionTarget类来完成,它提供了一个回调函数(Rendering的事件处理函数),WPF会在每次界面刷新时调用该回调函数。CompositionTarget的刷新率与窗体保持一致,因此很难人工控制动画的快慢。

案例:

效果图:

XAML:

<Window x:Class="Wpf基于桢的动画.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wpf基于桢的动画"
mc:Ignorable="d"
Title="MainWindow" Height="" Width="">
<Grid >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Ellipse Name="ellipse" Height="" Width="" Fill="Aquamarine"/>
<Button Grid.Row="" Content="开启动画" Height="" Width="" Click="ButtonBase_OnClick" />
</Grid>
</Window>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading; namespace Wpf基于桢的动画
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
CompositionTarget.Rendering+=new EventHandler(CompositionTarget_Rendering);
} private void CompositionTarget_Rendering(object sender, EventArgs e)
{
if (ellipse.Width < || ellipse.Height < )
{
ellipse.Width += ;
ellipse.Height += ;
}
else if (ellipse.Width >= || ellipse.Height >= )
{
CompositionTarget.Rendering-=new EventHandler(CompositionTarget_Rendering);
}
}
}
}

3.基于属性的动画

用一个DoubleAnimation类制定起始值(From=“”)、终点值To=“”、时间(Duration=“0:0:2.7”),以及动画结束应该(FillBehavior=”Stop”)。设置好之后该矩形调用BeginAnimation方法开始实现动画,BeginAnimation制定需要应用动画的属性和创建的DoubleAnimation。

案例:

效果图:

XAML:

<Window x:Class="Wpf基于属性的动画.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wpf基于属性的动画"
mc:Ignorable="d"
Title="MainWindow" Height="" Width="">
<Grid >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Ellipse Name="ellipse" Height="" Width="" Fill="Aquamarine"/>
<Button Grid.Row="" Content="开启动画" Height="" Width="" Click="ButtonBase_OnClick" />
</Grid>
</Window>

C#:

using System;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Shapes; namespace Wpf基于属性的动画
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
DoubleAnimation doubleAnimation=new DoubleAnimation();
doubleAnimation.From = ;
doubleAnimation.To = ;
doubleAnimation.Duration = TimeSpan.FromMilliseconds();
doubleAnimation.FillBehavior = FillBehavior.Stop;
ellipse.BeginAnimation(Ellipse.WidthProperty,doubleAnimation);
DoubleAnimation doubleAnimation1 = new DoubleAnimation();
doubleAnimation1.From = ;
doubleAnimation1.To = ;
doubleAnimation1.Duration = TimeSpan.FromMilliseconds();
doubleAnimation1.FillBehavior = FillBehavior.Stop;
ellipse.BeginAnimation(Ellipse.HeightProperty, doubleAnimation);
}
}
}

WPF实现动画的几种方式及其小案例的更多相关文章

  1. Spring框架访问数据库的两种方式的小案例

    1.1 以Xml的方式访问数据库的案例 要以xml的方式访问数据库需要用到JdbcTemplate ,因为 JdbcTemplate(jdbc的模板对象)在Spring 中提供了一个可以操作数据库的对 ...

  2. 加载gif动画的三种方式

    GifView.h/** * 调用结束就开始播放动画,如果需要用户指定何时播放的话,只需要把timer的开始放到合适的位置.通过对CFDictonaryRaf 也就是gifProperties的改变, ...

  3. WPF实现导航的几种方式

    下面是展示的是几种导航方式: 我们来具体看下xaml文件 <Page x:Class="WPF实现Navigation.Page1" xmlns="http://s ...

  4. 实现activity跳转动画的若干种方式

    第一种: (使用overridePendingTransition方法实现Activity跳转动画) 在Activity中代码如下 /** * 点击按钮实现跳转逻辑 */ button1.setOnC ...

  5. Android系统移植与调试之------->如何修改开机动画的两种方式剖析

    首先,我们先来分析一下源码: frameworks/base/cmds/bootanimation/BootAnimation.cpp 首先看一下定义的常量: BootAnimation::ready ...

  6. WPF设置样式的几种方式

    第一种方式是直接使用Setter来进行,可以对Background等进行设置. <Window.Resources> <Style TargetType="Button&q ...

  7. 前端制作动画的几种方式(css3,js)

    制作动态的网页是是前端工程师必备的技能,很好的实现动画能够极大的提高用户体验,增强交互效果,那么动画有多少实现方式,一直对此有选择恐惧症的我就总结一下,以便在开发的时候选择最好的实现方式. 1.css ...

  8. IOS 动画的两种方式

    方式一: [UIView animateWithDuration:1 animations:^{ //动画的内容 CGRect frame = CGRectMake([UIParam widthScr ...

  9. Cocos2d-x3.3beta0创建动画的3种方式

    1.单独载入精灵对象 渲染效率低,浪费资源,不推荐用该方法.代码例如以下:注:代码仅仅需贴到HelloWorldScene.cpp中就可以. //First,单独渲染每个精灵帧 auto sprite ...

随机推荐

  1. “全栈2019”Java多线程第十一章:线程优先级详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  2. Mybatis 优化:

    Mybatis 的优化: ** 第一个 对于数据库配置的优化: 创建一个 DB.properties 的文件 里面编写Key = value 形式的数据库信息 比如: driver = com.mys ...

  3. 虚拟机搭建ftp环境

    引用http://www.cnblogs.com/xiangxiaodong/archive/2013/12/23/3487028.html,学习. 本人是在windows8系统下,Oracle VM ...

  4. elasticsearch.net search入门使用指南中文版(翻译)

    elasticsearch.net search入门使用指南中文版,elasticsearch.Net是一个非常底层且灵活的客户端,它不在意你如何的构建自己的请求和响应.它非常抽象,因此所有的elas ...

  5. 【vim】插入模式与常用编辑操作

    vim不像很多编辑器那样一启动便可以直接编辑文本,需要在普通模式按下i, a等键才会进入插入模式进行文本编辑. 如何进入插入模式 以下的命令都会让vim从普通模式切换到插入模式,但命令执行后的字符插入 ...

  6. [科普] 借助 everything 扩展教你屏蔽网址或转发网址

    教你屏蔽网址或转发网址 万恶之源 为什么写这篇文章,俺觉得大家应该是有这个需(bai)求(du)的.只是不知道如何操作... 一.屏蔽网址 1.借助系统自带防火墙 (不推荐) Linux 下有 ipt ...

  7. Jmeter使用吞吐量控制器实现不同的用户操纵不同的业务

    一.需求 需求:博客系统,模拟用户真实行为,80%的用户阅读文章,20%的用户创建文章,创建文章的用户随机的删除或者修改文章. 二.脚本实现 80%的用户查看文章 20%用户创建文章 根据post_i ...

  8. RN47 中通过 JS 调用 Native 方法

    每一个模块.方法都有一个 ID,通过 ID 来调用. m_registry->callNativeMethod(call.moduleId, call.methodId, std::move(c ...

  9. QuantLib 金融计算——基本组件之 DayCounter 类

    目录 QuantLib 金融计算--基本组件之 DayCounter 类 DayCounter 对象的构造 一些常用的成员函数 如果未做特别说明,文中的程序都是 Python3 代码. QuantLi ...

  10. L04-VirtualBox中CentOS7网络配置(可连外网)

    本文所述的方法在RHEL6.5.RHEL7和CentOS6.5中同样适用. 1.工具:VirtualBox,虚拟机:CentOS7 2.VirtualBox工具中的网络配置 (1)VirtualBox ...