WPF Cover Flow Tutorial : Part 3

15 篇文章 0 订阅
In Part 2, we miss the animation part of the flow.

In the main TestWindow, we save all the covers in a List :
  1. private readonly List<Cover> coverList = new List<Cover>();  
  2. public TestWindow()  
  3. {  
  4.     InitializeComponent();  
  5.     var assembly = new FileInfo(Assembly.GetExecutingAssembly().Location);  
  6.     var image = new FileInfo(Path.Combine(assembly.Directory.FullName, "Katy Perry.jpg"));  
  7.     for (int i = 0; i < 10; i++)  
  8.     {  
  9.         var cover = new Cover(image.FullName, i);  
  10.         coverList.Add(cover);  
  11.         visualModel.Children.Add(cover);  
  12.     }  
  13. }  
We add an handler for the KeyDown event on the Window element. We will only deal with the Right and Leftkeys. Once one of these keys is pressed down, we animate the old current cover and the new one.
  1. private void RotateCover(int pos)  
  2. {  
  3.     coverList[pos].Animate(index);  
  4. }  
  5. private void UpdateIndex(int newIndex)  
  6. {  
  7.     if (index != newIndex)  
  8.     {  
  9.         int oldIndex = index;  
  10.         index = newIndex;  
  11.         RotateCover(oldIndex);  
  12.         RotateCover(index);  
  13.         camera.Position = new Point3D(.2 * index, camera.Position.Y, camera.Position.Z);  
  14.     }  
  15. }  
  16. private void Window_KeyDown(object sender, KeyEventArgs e)  
  17. {  
  18.     int newIndex = index;  
  19.     switch (e.Key)  
  20.     {  
  21.         case Key.Right:  
  22.             if (newIndex < coverList.Count - 1)  
  23.                 newIndex++;  
  24.             break;  
  25.         case Key.Left:  
  26.             if (newIndex > 0)  
  27.                 newIndex--;  
  28.             break;  
  29.     }  
  30.     UpdateIndex(newIndex);  
  31. }  
Currently, we do not animate covers. We just move them from one place to another. In order to get a real animation, we have to deal with Animation objects. In this new version of the Animate method, we ask the engine to animate the covers. As we have saved the translation and rotation objects in two Cover attributes, we can directly update their parameters (angle and offsets).
  1. public void Animate(int index)  
  2. {  
  3.     TimeSpan duration = TimeSpan.FromMilliseconds(500);  
  4.     var rotateAnimation = new DoubleAnimation(RotationAngle(index), duration);  
  5.     var xAnimation = new DoubleAnimation(TranslationX(index), duration);  
  6.     var zAnimation = new DoubleAnimation(TranslationZ(index), duration);  
  7.     rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, rotateAnimation);  
  8.     translation.BeginAnimation(TranslateTransform3D.OffsetXProperty, xAnimation);  
  9.     translation.BeginAnimation(TranslateTransform3D.OffsetZProperty, zAnimation);  
  10. }  

Download source. Continue with Part 4.

转载自:http://d3dal3.blogspot.com/2008/10/wpf-cover-flow-tutorial-part-3.html

版权归原作者所有。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值