自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Windows Phone開(kāi)發(fā)(42):緩動(dòng)動(dòng)畫(huà)

移動(dòng)開(kāi)發(fā)
我們看出來(lái)了,緩動(dòng)函數(shù)涉及復(fù)雜的數(shù)學(xué)運(yùn)算,不過(guò),灰常幸運(yùn)的是,常用的緩函數(shù)MS已經(jīng)為我們封裝好了,這也是從 WPF/Silverlight中遷移到WP的,集成性兼容性MD的好,所以,在使用的時(shí)候,大家可以輕松一大把了,因此,當(dāng)你在練習(xí)的時(shí)候,記得沖一杯 咖啡放在桌上,一邊寫(xiě)代碼一邊品嘗吧。呵呵,編程快樂(lè),快樂(lè)編程。

前面在討論關(guān)鍵幀動(dòng)畫(huà)的時(shí)候,我有意把幾個(gè)帶緩動(dòng)動(dòng)畫(huà)的關(guān)鍵幀動(dòng)畫(huà)忽略掉,如EasingColorKeyFrame、 EasingDoubleKeyFrame和EasingPointKeyFrame,其實(shí)為數(shù)不多,就這么幾個(gè)。因?yàn)槲蚁=y(tǒng)一放到這節(jié)課程來(lái)吹一下緩動(dòng) 函數(shù)。

所謂緩動(dòng)函數(shù),就是我們?cè)诖鷶?shù)里面說(shuō)的函數(shù),說(shuō)白了嘛,就是根特定的函數(shù)規(guī)則,用輸入的值算出最終值,使得動(dòng)畫(huà)在兩個(gè)關(guān)鍵幀之間不再是均衡過(guò)度,而是帶有加/減速或其他效果,當(dāng)然,還要取決于算法。

比如函數(shù)

所以,我們看出來(lái)了,緩動(dòng)函數(shù)涉及復(fù)雜的數(shù)學(xué)運(yùn)算,不過(guò),灰常幸運(yùn)的是,常用的緩函數(shù)MS已經(jīng)為我們封裝好了,這也是從 WPF/Silverlight中遷移到WP的,集成性兼容性MD的好,所以,在使用的時(shí)候,大家可以輕松一大把了,因此,當(dāng)你在練習(xí)的時(shí)候,記得沖一杯 咖啡放在桌上,一邊寫(xiě)代碼一邊品嘗吧。呵呵,編程快樂(lè),快樂(lè)編程。

如果你干過(guò)WPF或SL,這些東西你會(huì)覺(jué)得灰常Easy,我不是第一次強(qiáng)調(diào)了,所以說(shuō),基礎(chǔ)很重要,把基礎(chǔ)扎實(shí)了,無(wú)論你學(xué)習(xí)什么新玩意兒,都可以一通百通的,不管你信不信,反正我信了。

如何你對(duì)緩動(dòng)函數(shù)沒(méi)有一個(gè)直觀的認(rèn)識(shí)也不要緊,下面推薦大家一個(gè)游戲,很好玩的,不玩你學(xué)WP會(huì)后悔的。游戲地址:http://samples.msdn.microsoft.com/Silverlight/silverlight_next/Animations/easing_functions_gallery/testpage.html

記住要認(rèn)真玩,這樣你才會(huì)熟悉緩動(dòng)函數(shù)與相關(guān)的類(lèi)。

某致理名言說(shuō)得好,“自己動(dòng)手,豐衣足食”,還是老規(guī)矩,實(shí)例決定一切,動(dòng)手干活吧。

演練一

請(qǐng)參考以下XAML代碼構(gòu)建你的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>   

后臺(tái)C#代碼啟用動(dòng)畫(huà)。

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

現(xiàn)在,運(yùn)行上面的示例,你會(huì)發(fā)現(xiàn)圓在下落的過(guò)程發(fā)生了兩次回彈,動(dòng)畫(huà)才結(jié)束,而且一開(kāi)始較慢,后來(lái)漸漸加速,這就是緩動(dòng)函數(shù)所產(chǎn)生的效果。

演練二:

參考以下XAML代碼創(chuàng)建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上右擊,從彈出的菜單中選擇“導(dǎo)航到事件處理程序”,在生成的方法中完成以下C#代碼。

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

運(yùn)行程序后,你會(huì)看到漸變填充中間黃色的色塊在左右來(lái)回移動(dòng)。

演練三:

請(qǐng)參照以下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 是包含所有頁(yè)面內(nèi)容的根網(wǎng)格-->   
  16.         <Grid x:Name="LayoutRoot" Background="Transparent">   
  17.             <Grid.RowDefinitions>   
  18.                 <RowDefinition Height="Auto"/>   
  19.                 <RowDefinition Height="*"/>   
  20.             </Grid.RowDefinitions>   
  21.             <!--TitlePanel 包含應(yīng)用程序的名稱(chēng)和頁(yè)標(biāo)題-->   
  22.             <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">   
  23.                 <TextBlock x:Name="ApplicationTitle" Text="我的應(yīng)用程序" Style="{StaticResource PhoneTextNormalStyle}"/>   
  24.                 <TextBlock x:Name="PageTitle" Text="頁(yè)面名稱(chēng)" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>   
  25.             </StackPanel>   
  26.             <!--ContentPanel - 在此處放置其他內(nèi)容-->   
  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="請(qǐng)點(diǎn)擊左下角的白色矩形。"   
  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="你好,點(diǎn)擊下面的按鈕吧。"/>   
  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方法上右擊,從彈出的菜單中選擇“導(dǎo)航到事件處理程序”,完成后臺(tái)代碼邏輯。

  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. }   

現(xiàn)在,運(yùn)行程序,單擊屏幕左下方的白色矩形,這時(shí)候屏幕下方會(huì)彈出一個(gè)面板,再點(diǎn)擊面板上的按鈕,面板會(huì)縮下去。

這樣,使用動(dòng)畫(huà),我們就做出了一個(gè)類(lèi)似工具條的效果。

 

責(zé)任編輯:閆佳明 來(lái)源: oschina
相關(guān)推薦

2013-04-24 13:19:06

Windows Pho動(dòng)畫(huà)DoubleAni

2013-04-24 13:31:59

Windows Pho動(dòng)畫(huà)之ColorAni

2013-04-24 13:43:10

Windows Pho動(dòng)畫(huà)PointAnim

2013-07-30 12:37:56

Windows PhoWindows Pho

2010-04-21 17:07:54

Windows Pho

2013-04-24 13:51:48

Windows PhoWindows Pho

2013-04-24 14:52:53

Windows PhoWindows Pho

2013-04-24 15:28:02

Windows PhoWindows Pho

2021-01-31 17:34:01

Windows 10Windows微軟

2013-04-16 17:02:50

Windows Pho概論

2013-04-19 16:34:56

Windows PhoWindows 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

2009-02-23 09:19:33

windows 7啟動(dòng)畫(huà)面

2010-04-08 17:40:23

Windows Pho

2010-12-14 18:48:49

微軟

2013-07-31 13:13:50

Windows PhoMVVM模式

2012-06-04 14:47:58

Windows Pho

2013-04-19 15:35:54

Windows Pho隔離存儲(chǔ)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)