原文: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. Python 基于python编写一些算法程序等

    基于python编写一些算法程序等 by:授客 QQ:1033553122 QQ群:7156436 没特意去研究,只是这对群友在QQ群里(7156436)提出的一些小程序实现.编程题,算法.问题等,本 ...

  2. JMeter http(s)测试脚本录制器的使用

    JMeter http(s)测试脚本录制器的使用 by:授客 QQ:1033553122 http(s) Test Script Recorder允许Jmeter在你使用普通浏览器浏览web应用时,拦 ...

  3. 遇到npm报错read ECONNRESET怎么办

    遇到npm 像弱智一样报错怎么办 read ECONNRESET This is most likely not a problem with npm itselft 'proxy' config i ...

  4. IDEA插件清单

    zookeeper插件,方便查看zk节点信息 Maven Helper,方便解决jar包冲突 Free Mybatis plugin,自动映射mapper接口到对应查询statements gener ...

  5. Python中DataFrame去重

    # 去除重复行数据 keep:'first':保留重复行的第一行,'last':保留重复行的最后一行,False:删除所有重复行df = df.drop_duplicates( subset=['YJ ...

  6. 使用mysqladmin extended-status查看MySQL的运行状态脚本

    一个好用的使用mysqladmin extended-status查看MySQL的运行状态脚本: mysqladmin -P3306 -uroot -p -h127. -r -i extended-s ...

  7. Spring Boot 静态页面

    spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下 /static /public /resources /META-IN ...

  8. VSCode + PYQT5 + QtDesigner 环境搭建和测试

    目的:编写Python桌面应用程序. 备注:也可以选择VS2017+QtDesigner ,但更喜欢VSCode 第1步:安装PyQt5和PyQt5-tools pip3 install -i htt ...

  9. PHP Excel导入数据到MySQL数据库

    数据导出已经有了,怎么能没有数据导入呢,同样使用TP5框架,首先需要下载phpexcel.zip,放到第三方类库目录vendor目录下,然后有一个页面可以让你选择要导入的Excel文件,然后点击导入按 ...

  10. January 23rd, 2018 Week 04th Tuesday

    Remembrance is a form of meeting, forgetfulness is a form of freedom. 记忆是一种相遇,遗忘是一种自由. Cherish those ...