---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
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 ![](http://p.qpimg.cn/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Fwww.cnblogs.com%2FImages%2FOutliningIndicators%2FExpandedBlockStart.gif&size=0)
![](http://p.qpimg.cn/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Fwww.cnblogs.com%2FImages%2FOutliningIndicators%2FContractedBlock.gif&size=0)
{ ![](http://p.qpimg.cn/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Fwww.cnblogs.com%2FImages%2FOutliningIndicators%2FExpandedSubBlockStart.gif&size=0)
/**//// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : System.Windows.Window ![](http://p.qpimg.cn/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Fwww.cnblogs.com%2FImages%2FOutliningIndicators%2FExpandedSubBlockStart.gif&size=0)
{
private Grid gridRoot;
private Button buttonTest;
public Window1() ![](http://p.qpimg.cn/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Fwww.cnblogs.com%2FImages%2FOutliningIndicators%2FExpandedSubBlockStart.gif&size=0)
{
IniComponent();
}
private void IniComponent() ![](http://p.qpimg.cn/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Fwww.cnblogs.com%2FImages%2FOutliningIndicators%2FExpandedSubBlockStart.gif&size=0)
{
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) ![](http://p.qpimg.cn/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Fwww.cnblogs.com%2FImages%2FOutliningIndicators%2FExpandedSubBlockStart.gif&size=0)
{
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 ![](http://p.qpimg.cn/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Fwww.cnblogs.com%2FImages%2FOutliningIndicators%2FExpandedSubBlockStart.gif&size=0)
{
[STAThread]
public static void Main() ![](http://p.qpimg.cn/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Fwww.cnblogs.com%2FImages%2FOutliningIndicators%2FExpandedSubBlockStart.gif&size=0)
{
Window1 win = new Window1();
Application app = new Application();
app.Run(win);
}