Windows Phone开发(42):缓动动画

移动开发
我们看出来了,缓动函数涉及复杂的数学运算,不过,灰常幸运的是,常用的缓函数MS已经为我们封装好了,这也是从 WPF/Silverlight中迁移到WP的,集成性兼容性MD的好,所以,在使用的时候,大家可以轻松一大把了,因此,当你在练习的时候,记得冲一杯 咖啡放在桌上,一边写代码一边品尝吧。呵呵,编程快乐,快乐编程。

前面在讨论关键帧动画的时候,我有意把几个带缓动动画的关键帧动画忽略掉,如EasingColorKeyFrame、 EasingDoubleKeyFrame和EasingPointKeyFrame,其实为数不多,就这么几个。因为我希统一放到这节课程来吹一下缓动 函数。

所谓缓动函数,就是我们在代数里面说的函数,说白了嘛,就是根特定的函数规则,用输入的值算出最终值,使得动画在两个关键帧之间不再是均衡过度,而是带有加/减速或其他效果,当然,还要取决于算法。

比如函数

所以,我们看出来了,缓动函数涉及复杂的数学运算,不过,灰常幸运的是,常用的缓函数MS已经为我们封装好了,这也是从 WPF/Silverlight中迁移到WP的,集成性兼容性MD的好,所以,在使用的时候,大家可以轻松一大把了,因此,当你在练习的时候,记得冲一杯 咖啡放在桌上,一边写代码一边品尝吧。呵呵,编程快乐,快乐编程。

如果你干过WPF或SL,这些东西你会觉得灰常Easy,我不是第一次强调了,所以说,基础很重要,把基础扎实了,无论你学习什么新玩意儿,都可以一通百通的,不管你信不信,反正我信了。

如何你对缓动函数没有一个直观的认识也不要紧,下面推荐大家一个游戏,很好玩的,不玩你学WP会后悔的。游戏地址:http://samples.msdn.microsoft.com/Silverlight/silverlight_next/Animations/easing_functions_gallery/testpage.html

记住要认真玩,这样你才会熟悉缓动函数与相关的类。

某致理名言说得好,“自己动手,丰衣足食”,还是老规矩,实例决定一切,动手干活吧。

演练一

请参考以下XAML代码构建你的UI。

  1.     <Grid Loaded="gridLoaded">   
  2.         <Ellipse HorizontalAlignment="Left"   
  3.                  VerticalAlignment="Top"   
  4.                  Fill="Orange"   
  5.                  Width="100"   
  6.                  Height="100"   
  7.                  x:Name="elp">   
  8.             <Ellipse.RenderTransform>   
  9.                 <TranslateTransform x:Name="trm"/>   
  10.             </Ellipse.RenderTransform>   
  11.         </Ellipse>   
  12.         <Grid.Resources>   
  13.             <Storyboard x:Name="std">   
  14.                 <DoubleAnimationUsingKeyFrames Duration="0:0:8"   
  15. Storyboard.TargetName="trm"   
  16. Storyboard.TargetProperty="Y"   
  17. RepeatBehavior="30">   
  18.                     <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>   
  19.                     <EasingDoubleKeyFrame KeyTime="0:0:8" Value="485">   
  20.                         <EasingDoubleKeyFrame.EasingFunction>   
  21.                             <BounceEase Bounciness="3" Bounces="3"/>   
  22.                         </EasingDoubleKeyFrame.EasingFunction>   
  23.                     </EasingDoubleKeyFrame>   
  24.                 </DoubleAnimationUsingKeyFrames>   
  25.             </Storyboard>   
  26.         </Grid.Resources>   
  27.     </Grid>   

后台C#代码启用动画。

  1. private void gridLoaded(object sender, RoutedEventArgs e)   
  2. {   
  3.     this.std.Begin();   
  4. }   

现在,运行上面的示例,你会发现圆在下落的过程发生了两次回弹,动画才结束,而且一开始较慢,后来渐渐加速,这就是缓动函数所产生的效果。

演练二:

参考以下XAML代码创建UI界面。

  1.     <Grid Loaded="gridLoaded">   
  2.         <Rectangle Margin="35" x:Name="rec">   
  3.             <Rectangle.Fill>   
  4.                 <LinearGradientBrush x:Name="brs" StartPoint="0,0.5" EndPoint="1,0.5">   
  5.                     <GradientStop Color="Blue" Offset="0"/>   
  6.                     <GradientStop Color="Yellow" Offset="0.5"/>   
  7.                     <GradientStop Color="Blue" Offset="1"/>   
  8.                 </LinearGradientBrush>   
  9.             </Rectangle.Fill>   
  10.         </Rectangle>   
  11.         <Grid.Resources>   
  12.             <Storyboard x:Name="std">   
  13.                 <DoubleAnimationUsingKeyFrames Duration="0:0:10"   
  14. Storyboard.TargetName="brs"   
  15. Storyboard.TargetProperty="(LinearGradientBrush.GradientStops)[1].(GradientStop.Offset)"   
  16. RepeatBehavior="35"   
  17. AutoReverse="True">   
  18.                     <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0.2"/>   
  19.                     <EasingDoubleKeyFrame KeyTime="0:0:10" Value="0.8">   
  20.                         <EasingDoubleKeyFrame.EasingFunction>   
  21.                             <ElasticEase Oscillations="4" Springiness="1"/>   
  22.                         </EasingDoubleKeyFrame.EasingFunction>   
  23.                     </EasingDoubleKeyFrame>   
  24.                 </DoubleAnimationUsingKeyFrames>   
  25.             </Storyboard>   
  26.         </Grid.Resources>   
  27.     </Grid>   

在gridLoaded上右击,从弹出的菜单中选择“导航到事件处理程序”,在生成的方法中完成以下C#代码。

  1. private void gridLoaded(object sender, RoutedEventArgs e)   
  2. {   
  3.     std.Begin();   
  4. }   

运行程序后,你会看到渐变填充中间黄色的色块在左右来回移动。

演练三:

请参照以下XAML建立UI。

  1.     <phone:PhoneApplicationPage    
  2.         x:Class="Sample.Page3"   
  3.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  4.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  5.         xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"   
  6.         xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"   
  7.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
  8.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  9.         FontFamily="{StaticResource PhoneFontFamilyNormal}"   
  10.         FontSize="{StaticResource PhoneFontSizeNormal}"   
  11.         Foreground="{StaticResource PhoneForegroundBrush}"   
  12.         SupportedOrientations="Portrait" Orientation="Portrait"   
  13.         mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"   
  14.         shell:SystemTray.IsVisible="True">   
  15.         <!--LayoutRoot 是包含所有页面内容的根网格-->   
  16.         <Grid x:Name="LayoutRoot" Background="Transparent">   
  17.             <Grid.RowDefinitions>   
  18.                 <RowDefinition Height="Auto"/>   
  19.                 <RowDefinition Height="*"/>   
  20.             </Grid.RowDefinitions>   
  21.             <!--TitlePanel 包含应用程序的名称和页标题-->   
  22.             <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">   
  23.                 <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>   
  24.                 <TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>   
  25.             </StackPanel>   
  26.             <!--ContentPanel - 在此处放置其他内容-->   
  27.             <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">   
  28.                 <Rectangle HorizontalAlignment="Left" VerticalAlignment="Bottom"   
  29.                            Width="60" Height="45" Fill="White"   
  30.                            MouseLeftButtonDown="onShow"/>   
  31.                 <TextBlock HorizontalAlignment="Center"   
  32.                            VerticalAlignment="Center"   
  33.                            Text="请点击左下角的白色矩形。"   
  34.                            TextWrapping="Wrap"   
  35.                            Margin="30,0,40,0"   
  36.                            FontSize="45"/>   
  37.                 <StackPanel x:Name="sp" Background="LightBlue"   
  38.                             Height="180"   
  39.                             VerticalAlignment="Bottom">   
  40.                     <TextBlock Foreground="Red"    
  41.                                Margin="20,22,20,20"   
  42.                                FontSize="32"   
  43.                                Text="你好,点击下面的按钮吧。"/>   
  44.                     <Button Content="确    定" Background="Blue"   
  45.                             Click="onHide"/>   
  46.                     <StackPanel.RenderTransform>   
  47.                         <TranslateTransform x:Name="trm"   
  48.                                             Y="180"/>   
  49.                     </StackPanel.RenderTransform>   
  50.                 </StackPanel>   
  51.  
  52.                 <Grid.Resources>   
  53.                     <Storyboard x:Name="stdShow">   
  54.                         <DoubleAnimationUsingKeyFrames   
  55.                             Storyboard.TargetName="trm"   
  56.                             Storyboard.TargetProperty="Y"   
  57.                             Duration="1">   
  58.                             <EasingDoubleKeyFrame   
  59.                                 KeyTime="0:0:0" Value="180"/>   
  60.                             <EasingDoubleKeyFrame   
  61.                                 KeyTime="0:0:1" Value="0">   
  62. <EasingDoubleKeyFrame.EasingFunction>   
  63.                                     <PowerEase Power="10"/>   
  64. </EasingDoubleKeyFrame.EasingFunction>   
  65.                             </EasingDoubleKeyFrame>   
  66.                         </DoubleAnimationUsingKeyFrames>   
  67.                     </Storyboard>   
  68.                     <Storyboard x:Name="stdHide">   
  69.                         <DoubleAnimationUsingKeyFrames   
  70.                             Duration="0:0:1"   
  71.                             Storyboard.TargetName="trm"   
  72.                             Storyboard.TargetProperty="Y">   
  73.                             <EasingDoubleKeyFrame KeyTime="0:0:0"   
  74.                                                   Value="0"/>   
  75.                             <EasingDoubleKeyFrame KeyTime="0:0:1"   
  76.                                                   Value="180">   
  77. <EasingDoubleKeyFrame.EasingFunction>   
  78.                                     <PowerEase Power="10"/>   
  79. </EasingDoubleKeyFrame.EasingFunction>   
  80.                             </EasingDoubleKeyFrame>   
  81.                         </DoubleAnimationUsingKeyFrames>   
  82.                     </Storyboard>   
  83.                 </Grid.Resources>   
  84.             </Grid>   
  85.         </Grid>   
  86.     </phone:PhoneApplicationPage>   

分别在onShow和onHide方法上右击,从弹出的菜单中选择“导航到事件处理程序”,完成后台代码逻辑。

  1. private void onShow(object sender, MouseButtonEventArgs e)   
  2. {   
  3.     if (stdHide.GetCurrentState() != ClockState.Stopped)   
  4.     {   
  5.         stdHide.Stop();   
  6.     }   
  7.     stdShow.Begin();   
  8. }  
  9. private void onHide(object sender, RoutedEventArgs e)   
  10. {   
  11.     if (stdShow.GetCurrentState() != ClockState.Stopped)   
  12.     {   
  13.         stdShow.Stop();   
  14.     }   
  15.     stdHide.Begin();   
  16. }   

现在,运行程序,单击屏幕左下方的白色矩形,这时候屏幕下方会弹出一个面板,再点击面板上的按钮,面板会缩下去。

这样,使用动画,我们就做出了一个类似工具条的效果。

 

责任编辑:闫佳明 来源: oschina
相关推荐

2013-04-24 13:43:10

Windows Pho动画PointAnim

2013-04-24 13:19:06

Windows Pho动画DoubleAni

2013-04-24 13:31:59

Windows Pho动画之ColorAni

2013-04-24 15:28:02

Windows PhoWindows Pho

2013-04-24 13:51:48

Windows PhoWindows Pho

2013-04-24 14:52:53

Windows PhoWindows Pho

2010-04-21 17:07:54

Windows Pho

2013-07-30 12:37:56

Windows PhoWindows Pho

2021-01-31 17:34:01

Windows 10Windows微软

2009-02-23 09:19:33

windows 7启动画面

2013-04-19 16:34:56

Windows PhoWindows Pho

2013-04-16 17:02:50

Windows Pho概论

2013-07-30 11:18:37

Windows PhoWindows Pho

2011-06-07 12:42:15

Windows Pho

2013-04-17 14:00:06

Windows PhoWindows Pho

2010-04-08 17:40:23

Windows Pho

2010-12-14 18:48:49

微软

2013-04-19 16:52:24

Windows PhoWindows Pho

2013-07-31 13:13:50

Windows PhoMVVM模式

2013-04-19 15:35:54

Windows Pho隔离存储
点赞
收藏

51CTO技术栈公众号