WPF中的DoubleAnimation
WPF中的DoubleAnimation
周银辉
DoubleAnimation指定一个Double类型的属性,使其在指定的时间内由起点值到达终点值,从而形成动画效果.
应用它来实现动画效果,只要简单地指定几个参数值就可以了.
看下面的代码是改变一个按钮的大小的动画
//指定长度变化的起点,终点与持续时间
DoubleAnimation widthAnimation =
new DoubleAnimation(200, 400, new Duration(TimeSpan.FromSeconds(0.8)));
//指定高度变化的起点,终点与持续时间
DoubleAnimation heightAnimation =
new DoubleAnimation(50, 100, new Duration(TimeSpan.FromSeconds(0.8)));
//开始动画
//变化不是阻塞的,而是异步,所以看上去长度与高度几乎是同时变化
btn.BeginAnimation(Button.WidthProperty, widthAnimation);
btn.BeginAnimation(Button.HeightProperty, heightAnimation);这样我们就可以得到一个简单的动画,它在0.8秒内将按钮的长度由200变化到400,高度由50变化到100.
但我们会发现当动画结束后,按钮的大小保持在(400,100), 如果我们需要动画结束后将按钮大小恢复到原大小,那么我们应该指定另外一个参数:
FillBehavior
//指定长度变化的起点,终点与持续时间,并在动画结束时恢复原值
DoubleAnimation widthAnimation =
new DoubleAnimation(200, 400, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.Stop);
//指定高度变化的起点,终点与持续时间,并在动画结束时恢复原值
DoubleAnimation heightAnimation =
new DoubleAnimation(50, 100, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.Stop);以下是完整的实例代码:
using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;
using System.Windows.Media.Animation;

namespace DoubleAnimationTest

{
/**//// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : System.Windows.Window
{
private Grid gridRoot;
private Button buttonTest;
public Window1()
{
IniComponent();
}
private void IniComponent()
{
this.gridRoot = new Grid();
this.buttonTest = new Button();
this.buttonTest.Content = "this is a test button";
this.buttonTest.Width = 200;
this.buttonTest.Height = 50;
this.buttonTest.Click += new RoutedEventHandler(buttonTest_Click);
this.gridRoot.Children.Add(this.buttonTest);
this.Content = gridRoot;
}
void buttonTest_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
//指定长度变化的起点,终点与持续时间,并在动画结束时保持大小
DoubleAnimation widthAnimation =
new DoubleAnimation(200, 400, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.HoldEnd);
//指定高度变化的起点,终点与持续时间,并在动画结束时保持大小
DoubleAnimation heightAnimation =
new DoubleAnimation(50, 100, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.HoldEnd);
//开始动画
//变化不是阻塞的,而是异步,所以看上去长度与高度几乎是同时变化
btn.BeginAnimation(Button.WidthProperty, widthAnimation);
btn.BeginAnimation(Button.HeightProperty, heightAnimation);
}
}

public class MainClass
{
[STAThread]
public static void Main()
{
Window1 win = new Window1();
Application app = new Application();
app.Run(win);
}
}
}WPF中的DoubleAnimation的更多相关文章
- WPF中的动画——(三)时间线(TimeLine)
WPF中的动画——(三)时间线(TimeLine) 时间线(TimeLine)表示时间段. 它提供的属性可以让控制该时间段的长度.开始时间.重复次数.该时间段内时间进度的快慢等等.在WPF中内置了如下 ...
- wpf中的触发器详解
原文 http://zwkufo.blog.163.com/blog/static/25882512009724113250883/ 7.1.2 简单逻辑的表示--触发器(1) 在本章的多处介绍中都会 ...
- WPF: WPF 中的 Triggers 和 VisualStateManager
在之前写的这篇文章 WPF: 只读依赖属性的介绍与实践 中,我们介绍了在 WPF 自定义控件中如何添加只读依赖属性,并且使其结合属性触发器 (Trigger) 来实现对控件样式的改变.事实上,关于触发 ...
- WPF中触发器Trigger、MultiTrigger、DataTrigger、MultiDataTrigger、EventTrigger几种
WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改外观属性,或者执行动画等操作. WPFtrigger的主要类型有:Trigger. ...
- WPF中反转3D列表项
原文:WPF中反转3D列表项 WPF中反转3D列表项 周银辉记得在苹果电脑中有一个很酷的 ...
- (转载)WPF中的动画——(一)基本概念
http://www.cnblogs.com/TianFang/p/4050845.html WPF的一个特点就是支持动画,我们可以非常容易的实现漂亮大方的界面.首先,我们来复习一下动画的基本概念.计 ...
- WPF中的动画
动画无疑是WPF中最吸引人的特色之一,其可以像Flash一样平滑地播放并与程序逻辑进行很好的交互.这里我们讨论一下故事板. 在WPF中我们采用Storyboard(故事板)的方式来编写动画,为了对St ...
- WPF中的动画——(一)基本概念
WPF的一个特点就是支持动画,我们可以非常容易的实现漂亮大方的界面.首先,我们来复习一下动画的基本概念.计算机中的动画一般是定格动画,也称之为逐帧动画,它通过每帧不同的图像连续播放,从而欺骗眼和脑产生 ...
- wpf中的触发器详解 (转自 乂乂的日志 - 网易博客)
2010-03-24 16:19:07| 分类: WPF相关 | 标签: |字号大中小 订阅 wpf中的触发器详解 WPF/C# 2009-08-24 11:32:50 7.1.2 简单 ...
随机推荐
- 自定义控件详解(一):Paint类与Canvas类
前言: 自定义控件必需的两个类:Paint与Canvas Paint --- 相当于绘图的"笔" Canvas --- 相当于绘图的"纸" 一.Pain ...
- 《Inside C#》笔记(五) 方法
方法用来体现类的行为. 一 相关概念 a) ref和out 通常一个方法只能返回一个值,但如果确实需要返回多个值时,可以使用ref或out.如果方法的参数被标注为ref或out,在调用该方法时,会传递 ...
- Android Stuido代码混淆
一.Android Studio 代码混淆基本配置首先我们要在build.gradle里设置 miifyEnabled 里改为true,表示可以混淆 proguardFiles getDefaultP ...
- Socks5 RFC1928协议中文版
除了这个意译版rfc1928外,其他人写的好像也有错误,都是一知半解. ☆ RFC 1928意译版(非直译版) http://www.ietf.org/rfc/rfc1928.txt http://w ...
- [20171225]变态的windows批处理4.txt
[20171225]变态的windows批处理4.txt --//昨天学习windows 批处理的echo &.使用它可以实现类似回车换行的功能.例子: 1.echo &.R:\> ...
- 洗礼灵魂,修炼python(25)--自定义函数(6)—从匿名函数进阶话题讲解中解析“函数式编程”
匿名函数进阶 前一章已经说了匿名函数,匿名函数还可以和其他内置函数结合使用 1.map map():映射器,映射 list(map(lambda x:x*2,range(10))) #把range产生 ...
- 华硕200系主板完美兼容M.2安装Win7系统
虽然Windows 10系统的装机率正不断攀升,但经典的Windows 7依然有着大量的用户群体.特别是在我们中国, Windows 7依然是许许多多电脑用户的装机首选系统. 经久不衰的Windows ...
- 【PAT】B1062 最简分数(20 分)
如果了解分数运算,本题很简单.我有对分数知识进行总结 分数四则运算 #include<stdio.h> #include<algorithm> using namespace ...
- svg 认识及动画
svg在线中文教程 https://svg.brucewar.me/ http://www.zhangxinxu.com/wordpress/tag/svg/ http://svgtrick.com ...
- Eclipse 报错The method xxx of type must override a superclass method、Description Resource Path Location Type Java compiler level does not match the version of the installed Java project facet
问题: 如上图, 没改钱@Override会报错The method run() of type must override a superclass method 原因: java1.5中继承接口是 ...