原文: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的更多相关文章

  1. WPF中的动画——(三)时间线(TimeLine)

    WPF中的动画——(三)时间线(TimeLine) 时间线(TimeLine)表示时间段. 它提供的属性可以让控制该时间段的长度.开始时间.重复次数.该时间段内时间进度的快慢等等.在WPF中内置了如下 ...

  2. wpf中的触发器详解

    原文 http://zwkufo.blog.163.com/blog/static/25882512009724113250883/ 7.1.2 简单逻辑的表示--触发器(1) 在本章的多处介绍中都会 ...

  3. WPF: WPF 中的 Triggers 和 VisualStateManager

    在之前写的这篇文章 WPF: 只读依赖属性的介绍与实践 中,我们介绍了在 WPF 自定义控件中如何添加只读依赖属性,并且使其结合属性触发器 (Trigger) 来实现对控件样式的改变.事实上,关于触发 ...

  4. WPF中触发器Trigger、MultiTrigger、DataTrigger、MultiDataTrigger、EventTrigger几种

    WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改外观属性,或者执行动画等操作. WPFtrigger的主要类型有:Trigger. ...

  5. WPF中反转3D列表项

    原文:WPF中反转3D列表项 WPF中反转3D列表项                                                         周银辉记得在苹果电脑中有一个很酷的 ...

  6. (转载)WPF中的动画——(一)基本概念

    http://www.cnblogs.com/TianFang/p/4050845.html WPF的一个特点就是支持动画,我们可以非常容易的实现漂亮大方的界面.首先,我们来复习一下动画的基本概念.计 ...

  7. WPF中的动画

    动画无疑是WPF中最吸引人的特色之一,其可以像Flash一样平滑地播放并与程序逻辑进行很好的交互.这里我们讨论一下故事板. 在WPF中我们采用Storyboard(故事板)的方式来编写动画,为了对St ...

  8. WPF中的动画——(一)基本概念

    WPF的一个特点就是支持动画,我们可以非常容易的实现漂亮大方的界面.首先,我们来复习一下动画的基本概念.计算机中的动画一般是定格动画,也称之为逐帧动画,它通过每帧不同的图像连续播放,从而欺骗眼和脑产生 ...

  9. wpf中的触发器详解 (转自 乂乂的日志 - 网易博客)

    2010-03-24 16:19:07|  分类: WPF相关 |  标签: |字号大中小 订阅     wpf中的触发器详解 WPF/C# 2009-08-24 11:32:50 7.1.2  简单 ...

随机推荐

  1. Linux服务器tomcat启动maven项目

    在本地的IDEA写了一个简单的maven项目,仅展示一个页面.之后将项目git push到服务器上. 在服务器git pull拉取(第一次需要clone),之后将项目打包编译后的做法如下: 前提(服务 ...

  2. (其他)最常用的15大Eclipse开发快捷键技巧

    转自CSDNJava我人生(陈磊兴)   原文出处 引言 做java开发的,经常会用Eclipse或者MyEclise集成开发环境,一些实用的Eclipse快捷键和使用技巧,可以在平常开发中节约出很多 ...

  3. maven(八),阿里云国内镜像,提高jar包下载速度

    镜像 maven默认会从中央仓库下载jar包,这个仓库在国外,而且全世界的人都会从这里下载,所以下载速度肯定是非常慢的.镜像就相当于是中央仓库的一个副本,内容和中央仓库完全一样,目前有不少国内镜像,其 ...

  4. [20180608]Wrong Results with IOT, Added Column and Secondary Index.txt

    [20180608]Wrong Results with IOT, Added Column and Secondary Index.txt --//链接:http://db-oriented.com ...

  5. 大表分批删除脚本之MySQL版

    经常需要定期对某些表删除历史数据,通常这样的表的数据又是非常巨大,为了减轻对线上环境的影响,删除时必须分成小批量来进行. 以前分享过SQLServer的版本. 下面是MySQL版本: delimite ...

  6. Sql Server中查询当天,最近三天,本周,本月,最近一个月,本季度的数据的sql语句

    --当天: --最近三天: --本周: select * from T_news WHERE (DATEPART(wk, addtime) = DATEPART(wk, GETDATE())) AND ...

  7. 无需软件windows如何加密文件夹

    在百部百科上看到,放在博客中以便查看. 1.首先打开记事本,当然如果你的电脑里装有类似notepad++的文本编辑软件的也可以,但是不能用word.用这类软件好处是代码高亮,看上去舒服,减少错误率. ...

  8. 【PAT】B1041 考试座位号(15 分)

    /* */ #include<stdio.h> #include<algorithm> using namespace std; struct stu{ char number ...

  9. [项目实践] python文件路径引用的规则,记一次使用sys.path[0]的问题,及如何区分 ../与 ./的使用场景

    下面是一个获取配置的代码 def getValue(self,section,option): """ @file: string,the name of the con ...

  10. 写给spring版本的那些事儿

    1.远程调用rmi协议 Exception in thread "main" java.rmi.UnmarshalException: error unmarshalling re ...