WPF Cover Flow Tutorial : Part 3

来源:互联网 发布:移动dvd播放机网络mp5 编辑:程序博客网 时间:2024/05/01 10:30
In Part 2, we miss the animation part of the flow.

In the main TestWindow, we save all the covers in a List :
view plainprint?
  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.
view plainprint?
  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).
view plainprint?
  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

版权归原作者所有。