稳扎稳打Silverlight(37) - 3.0动画之Easing(缓动效果)


[索引页]

[源码下载]


稳扎稳打Silverlight(37) - 3.0动画之Easing(缓动效果)


作者: webabcd


介绍
Silverlight 3.0 动画的缓动效果:
  • 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)


在线DEMO
http://webabcd.blog.51cto.com/1787395/342289


示例
1、Silverlight 3.0 内置的 11 种缓动效果的 Demo
Easing.xaml
<navigation:Page x:Class="Silverlight30.Animation.Easing"    
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     mc:Ignorable="d" 
                     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
                     d:DesignWidth="640" d:DesignHeight="480" 
                     Title="Easing Page"> 
        <Grid x:Name="LayoutRoot"> 
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"> 

                        <!--用于显示各种 Easing 的图例列表--> 
                        <StackPanel Margin="10"> 
                                <ListBox x:Name="lstEasing"> 
                                        <ListBox.ItemTemplate> 
                                                <DataTemplate> 
                                                        <StackPanel Margin="1"> 
                                                                <TextBlock Text="{Binding EasingName}" /> 
                                                                <Image Source="{Binding PicAddress}" Width="300" Height="50" /> 
                                                        </StackPanel> 
                                                </DataTemplate> 
                                        </ListBox.ItemTemplate> 
                                </ListBox> 
                        </StackPanel> 

                        <StackPanel Margin="10, 200"> 
                        
                                <!--分别用 3 种动画来演示各类 Easing--> 
                                <ComboBox x:Name="cboTransform" Margin="5"> 
                                        <ComboBoxItem Content="Translate" IsSelected="True" /> 
                                        <ComboBoxItem Content="Rotate" /> 
                                        <ComboBoxItem Content="Scale" /> 
                                </ComboBox> 
                                 
                                <!--用各种 EasingMode 分别做演示--> 
                                <ComboBox x:Name="cboEasingMode" Margin="5"> 
                                        <ComboBoxItem Content="EaseOut" IsSelected="True" /> 
                                        <ComboBoxItem Content="EaseIn" /> 
                                        <ComboBoxItem Content="EaseInOut" /> 
                                </ComboBox> 

                                <Button x:Name="btnShow" Content="演示" Click="btnShow_Click" Margin="5" /> 

                                <!--用于做动画演示的矩形--> 
                                <Rectangle x:Name="rect" Fill="Blue" Width="200" Height="40" Margin="5" RenderTransformOrigin="0.5,0.5"> 
                                        <Rectangle.RenderTransform> 
                                                <TransformGroup> 
                                                        <TranslateTransform x:Name="tt" X="0" Y="0" /> 
                                                        <RotateTransform x:Name="rt" Angle="0" /> 
                                                        <ScaleTransform x:Name="st" ScaleX="1" ScaleY="1" /> 
                                                </TransformGroup> 
                                        </Rectangle.RenderTransform> 
                                </Rectangle> 

                        </StackPanel> 
                </StackPanel> 
        </Grid> 
</navigation:Page>
 
Easing.xaml.cs
InBlock.gif /* 
InBlock.gif * Easing - 与 Storyboard 结合实现动画的缓动效果 
InBlock.gif * Silverlight 3 内置 11 种缓动效果:分别为BackEase, BounceEase, CircleEase, CubicEase, ElasticEase, ExponentialEase, PowerEase, QuadraticEase, QuarticEase, QuinticEase, SineEase 
InBlock.gif * 各个缓动类都继承自 EasingFunctionBase,除了 EasingFunctionBase 提供的功能外,各个缓动类可能还会有各自的属性(懒的写了,查文档吧) 
InBlock.gif * EasingFunctionBase 有一个用于设置缓动模式的枚举类型属性 EasingMode (EasingMode.EaseOut(默认值), EasingMode.EaseIn, EasingMode.EaseInOut) 
InBlock.gif */
 
InBlock.gif 
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif using Silverlight30.Model; 
InBlock.gif using System.Xml.Linq; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Animation 
InBlock.gif
InBlock.gif         public partial  class Easing : Page 
InBlock.gif        { 
InBlock.gif                 public Easing() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(Easing_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 // 绑定各种 Easing 的图例列表 
InBlock.gif                 void Easing_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        XElement root = XElement.Load( "Animation/Easing.xml"); 
InBlock.gif                        var easings = from n  in root.Elements( "easing"
InBlock.gif                                                    select  new EasingModel 
InBlock.gif                                                    { 
InBlock.gif                                                            EasingName = ( string)n.Attribute( "EasingName"), 
InBlock.gif                                                            Description = ( string)n.Attribute( "Description"), 
InBlock.gif                                                            PicAddress = ( string)n.Attribute( "PicAddress"
InBlock.gif                                                    }; 
InBlock.gif 
InBlock.gif                        lstEasing.ItemsSource = easings; 
InBlock.gif                        lstEasing.SelectedIndex = 0; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private Storyboard _prevStoryboard; 
InBlock.gif 
InBlock.gif                 private  void btnShow_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         if (_prevStoryboard !=  null
InBlock.gif                                _prevStoryboard.Stop(); 
InBlock.gif 
InBlock.gif                         // 实例化用户所选择的 Easing 
InBlock.gif                        Type type =  typeof(EasingFunctionBase).Assembly.GetType( "System.Windows.Media.Animation." + ((EasingModel)lstEasing.SelectedItem).EasingName,  false); 
InBlock.gif                        EasingFunctionBase easing = Activator.CreateInstance(type)  as EasingFunctionBase; 
InBlock.gif 
InBlock.gif                         // 根据用户的选择,设置 Easing 的 EasingMode 属性 
InBlock.gif                        easing.EasingMode = (EasingMode)Enum.Parse( typeof(EasingMode), ((ComboBoxItem)cboEasingMode.SelectedItem).Content.ToString(),  true); 
InBlock.gif 
InBlock.gif                        Storyboard sb =  new Storyboard(); 
InBlock.gif                        _prevStoryboard = sb; 
InBlock.gif 
InBlock.gif                        var transformType = ((ComboBoxItem)cboTransform.SelectedItem).Content.ToString(); 
InBlock.gif                         switch (transformType) 
InBlock.gif                        { 
InBlock.gif                                 // 位移动画结合 Easing 的演示 
InBlock.gif                                 case  "Translate"
InBlock.gif                                        DoubleAnimation daTranslateY =  new DoubleAnimation(); 
InBlock.gif                                        daTranslateY.From = 0 ; 
InBlock.gif                                        daTranslateY.To = 200; 
InBlock.gif                                        daTranslateY.Duration = TimeSpan.FromSeconds(3); 
InBlock.gif 
InBlock.gif                                        daTranslateY.EasingFunction = easing; 
InBlock.gif 
InBlock.gif                                        Storyboard.SetTargetProperty(daTranslateY,  new PropertyPath( "Y")); 
InBlock.gif                                        Storyboard.SetTarget(daTranslateY, tt); 
InBlock.gif 
InBlock.gif                                        sb.Children.Add(daTranslateY); 
InBlock.gif 
InBlock.gif                                         break
InBlock.gif                                 // 缩放动画结合 Easing 的演示 
InBlock.gif                                 case  "Scale"
InBlock.gif                                        DoubleAnimation daScaleX =  new DoubleAnimation(); 
InBlock.gif                                        daScaleX.From = 1; 
InBlock.gif                                        daScaleX.To = 2; 
InBlock.gif                                        daScaleX.Duration = TimeSpan.FromSeconds(3); 
InBlock.gif 
InBlock.gif                                        DoubleAnimation daScaleY =  new DoubleAnimation(); 
InBlock.gif                                        daScaleY.From = 1; 
InBlock.gif                                        daScaleY.To = 2; 
InBlock.gif                                        daScaleY.Duration = TimeSpan.FromSeconds(3); 
InBlock.gif 
InBlock.gif                                        daScaleX.EasingFunction = easing; 
InBlock.gif                                        daScaleY.EasingFunction = easing; 
InBlock.gif 
InBlock.gif                                        Storyboard.SetTargetProperty(daScaleX,  new PropertyPath( "ScaleX")); 
InBlock.gif                                        Storyboard.SetTarget(daScaleX, st); 
InBlock.gif                                        Storyboard.SetTargetProperty(daScaleY,  new PropertyPath( "ScaleY")); 
InBlock.gif                                        Storyboard.SetTarget(daScaleY, st); 
InBlock.gif 
InBlock.gif                                        sb.Children.Add(daScaleX); 
InBlock.gif                                        sb.Children.Add(daScaleY); 
InBlock.gif 
InBlock.gif                                         break
InBlock.gif                                 // 旋转动画结合 Easing 的演示 
InBlock.gif                                 case  "Rotate"
InBlock.gif                                        DoubleAnimation daAngle =  new DoubleAnimation(); 
InBlock.gif                                        daAngle.To = 0; 
InBlock.gif                                        daAngle.To = 360; 
InBlock.gif                                        daAngle.Duration = TimeSpan.FromSeconds(3); 
InBlock.gif 
InBlock.gif                                        daAngle.EasingFunction = easing; 
InBlock.gif 
InBlock.gif                                        Storyboard.SetTargetProperty(daAngle,  new PropertyPath( "Angle")); 
InBlock.gif                                        Storyboard.SetTarget(daAngle, rt); 
InBlock.gif 
InBlock.gif                                        sb.Children.Add(daAngle); 
InBlock.gif 
InBlock.gif                                         break
InBlock.gif                        } 
InBlock.gif        
InBlock.gif                        sb.Begin(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
2、自定义缓动效果的 Demo
MyCustomEasing.cs
InBlock.gif using System; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Ink; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Animation 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 自定义缓动效果 
InBlock.gif         /// </summary> 
InBlock.gif         public  class MyCustomEasing : EasingFunctionBase 
InBlock.gif        { 
InBlock.gif                 public MyCustomEasing() 
InBlock.gif                        :  base() 
InBlock.gif                { 
InBlock.gif    
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 实现 EaseIn 模式下的逻辑 
InBlock.gif                 /// EaseOut 和 EaseInOut 会自动实现 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="normalizedTime">标准时间位(0 - 1之间)。即 动画运行到此的时间 占 动画运行所需的全部时间 的百分比</param> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 protected  override  double EaseInCore( double normalizedTime) 
InBlock.gif                { 
InBlock.gif                         // QuinticEase 效果的实现算法 
InBlock.gif 
InBlock.gif                         // 假定动画运行的总共时间为 1 秒 
InBlock.gif                         // 那么当 normalizedTime 为 0.1 时,动画运行到的位置为无该缓动效果时,0.00001秒后的位置。以此类推 
InBlock.gif                         return Math.Pow(normalizedTime, 5); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
CustomEasing.xaml
<navigation:Page x:Class="Silverlight30.Animation.CustomEasing"    
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     mc:Ignorable="d" 
                     xmlns:custom="clr-namespace:Silverlight30.Animation" 
                     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
                     d:DesignWidth="640" d:DesignHeight="480" 
                     Title="CustomEasing Page"> 
        <Grid x:Name="LayoutRoot"> 
                <StackPanel> 
                 
                        <StackPanel.Resources> 
                                <Storyboard x:Name="ani"> 
                                        <DoubleAnimation    
                                                Storyboard.TargetName="tt" 
                                                Storyboard.TargetProperty="Y" 
                                                From="0"    
                                                To="200"    
                                                Duration="00:00:03" 
                                        > 
                                                <DoubleAnimation.EasingFunction> 
                                                        <!--使用自定义的缓动效果--> 
                                                        <custom:MyCustomEasing EasingMode="EaseOut" /> 
                                                </DoubleAnimation.EasingFunction> 
                                        </DoubleAnimation> 
                                </Storyboard> 
                        </StackPanel.Resources> 

                        <Button x:Name="btnShow" Content="演示" Margin="5" Click="btnShow_Click" /> 

                        <Rectangle x:Name="rect" Fill="Blue" Width="200" Height="40" Margin="5" RenderTransformOrigin="0.5,0.5"> 
                                <Rectangle.RenderTransform> 
                                        <TransformGroup> 
                                                <TranslateTransform x:Name="tt" X="0" Y="0" /> 
                                        </TransformGroup> 
                                </Rectangle.RenderTransform> 
                        </Rectangle> 
                         
                </StackPanel> 
        </Grid> 
</navigation:Page>
 
CustomEasing.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Animation 
InBlock.gif
InBlock.gif         public partial  class CustomEasing : Page 
InBlock.gif        { 
InBlock.gif                 public CustomEasing() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnShow_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        ani.Begin(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
      本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/342758 ,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值