/*
* Easing - 与 Storyboard 结合实现动画的缓动效果
* Silverlight 3 内置 11 种缓动效果:分别为BackEase, BounceEase, CircleEase, CubicEase, ElasticEase, ExponentialEase, PowerEase, QuadraticEase, QuarticEase, QuinticEase, SineEase
* 各个缓动类都继承自 EasingFunctionBase,除了 EasingFunctionBase 提供的功能外,各个缓动类可能还会有各自的属性(懒的写了,查文档吧)
* EasingFunctionBase 有一个用于设置缓动模式的枚举类型属性 EasingMode (EasingMode.EaseOut(默认值), EasingMode.EaseIn, EasingMode.EaseInOut)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Silverlight30.Model;
using System.Xml.Linq;
namespace Silverlight30.Animation
{
public partial class Easing : Page
{
public Easing()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Easing_Loaded);
}
// 绑定各种 Easing 的图例列表
void Easing_Loaded( object sender, RoutedEventArgs e)
{
XElement root = XElement.Load( "Animation/Easing.xml");
var easings = from n in root.Elements( "easing")
select new EasingModel
{
EasingName = ( string)n.Attribute( "EasingName"),
Description = ( string)n.Attribute( "Description"),
PicAddress = ( string)n.Attribute( "PicAddress")
};
lstEasing.ItemsSource = easings;
lstEasing.SelectedIndex = 0;
}
private Storyboard _prevStoryboard;
private void btnShow_Click( object sender, RoutedEventArgs e)
{
if (_prevStoryboard != null)
_prevStoryboard.Stop();
// 实例化用户所选择的 Easing
Type type = typeof(EasingFunctionBase).Assembly.GetType( "System.Windows.Media.Animation." + ((EasingModel)lstEasing.SelectedItem).EasingName, false);
EasingFunctionBase easing = Activator.CreateInstance(type) as EasingFunctionBase;
// 根据用户的选择,设置 Easing 的 EasingMode 属性
easing.EasingMode = (EasingMode)Enum.Parse( typeof(EasingMode), ((ComboBoxItem)cboEasingMode.SelectedItem).Content.ToString(), true);
Storyboard sb = new Storyboard();
_prevStoryboard = sb;
var transformType = ((ComboBoxItem)cboTransform.SelectedItem).Content.ToString();
switch (transformType)
{
// 位移动画结合 Easing 的演示
case "Translate":
DoubleAnimation daTranslateY = new DoubleAnimation();
daTranslateY.From = 0 ;
daTranslateY.To = 200;
daTranslateY.Duration = TimeSpan.FromSeconds(3);
daTranslateY.EasingFunction = easing;
Storyboard.SetTargetProperty(daTranslateY, new PropertyPath( "Y"));
Storyboard.SetTarget(daTranslateY, tt);
sb.Children.Add(daTranslateY);
break;
// 缩放动画结合 Easing 的演示
case "Scale":
DoubleAnimation daScaleX = new DoubleAnimation();
daScaleX.From = 1;
daScaleX.To = 2;
daScaleX.Duration = TimeSpan.FromSeconds(3);
DoubleAnimation daScaleY = new DoubleAnimation();
daScaleY.From = 1;
daScaleY.To = 2;
daScaleY.Duration = TimeSpan.FromSeconds(3);
daScaleX.EasingFunction = easing;
daScaleY.EasingFunction = easing;
Storyboard.SetTargetProperty(daScaleX, new PropertyPath( "ScaleX"));
Storyboard.SetTarget(daScaleX, st);
Storyboard.SetTargetProperty(daScaleY, new PropertyPath( "ScaleY"));
Storyboard.SetTarget(daScaleY, st);
sb.Children.Add(daScaleX);
sb.Children.Add(daScaleY);
break;
// 旋转动画结合 Easing 的演示
case "Rotate":
DoubleAnimation daAngle = new DoubleAnimation();
daAngle.To = 0;
daAngle.To = 360;
daAngle.Duration = TimeSpan.FromSeconds(3);
daAngle.EasingFunction = easing;
Storyboard.SetTargetProperty(daAngle, new PropertyPath( "Angle"));
Storyboard.SetTarget(daAngle, rt);
sb.Children.Add(daAngle);
break;
}
sb.Begin();
}
}
}