In Part 2, we miss the animation part of the flow.
In the main TestWindow, we save all the covers in a List :
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.
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).
In the main TestWindow, we save all the covers in a List :
- private readonly List<Cover> coverList = new List<Cover>();
- public TestWindow()
- {
- InitializeComponent();
- var assembly = new FileInfo(Assembly.GetExecutingAssembly().Location);
- var image = new FileInfo(Path.Combine(assembly.Directory.FullName, "Katy Perry.jpg"));
- for (int i = 0; i < 10; i++)
- {
- var cover = new Cover(image.FullName, i);
- coverList.Add(cover);
- visualModel.Children.Add(cover);
- }
- }
- private void RotateCover(int pos)
- {
- coverList[pos].Animate(index);
- }
- private void UpdateIndex(int newIndex)
- {
- if (index != newIndex)
- {
- int oldIndex = index;
- index = newIndex;
- RotateCover(oldIndex);
- RotateCover(index);
- camera.Position = new Point3D(.2 * index, camera.Position.Y, camera.Position.Z);
- }
- }
- private void Window_KeyDown(object sender, KeyEventArgs e)
- {
- int newIndex = index;
- switch (e.Key)
- {
- case Key.Right:
- if (newIndex < coverList.Count - 1)
- newIndex++;
- break;
- case Key.Left:
- if (newIndex > 0)
- newIndex--;
- break;
- }
- UpdateIndex(newIndex);
- }
- public void Animate(int index)
- {
- TimeSpan duration = TimeSpan.FromMilliseconds(500);
- var rotateAnimation = new DoubleAnimation(RotationAngle(index), duration);
- var xAnimation = new DoubleAnimation(TranslationX(index), duration);
- var zAnimation = new DoubleAnimation(TranslationZ(index), duration);
- rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, rotateAnimation);
- translation.BeginAnimation(TranslateTransform3D.OffsetXProperty, xAnimation);
- translation.BeginAnimation(TranslateTransform3D.OffsetZProperty, zAnimation);
- }
Download source. Continue with Part 4.
转载自:http://d3dal3.blogspot.com/2008/10/wpf-cover-flow-tutorial-part-3.html
版权归原作者所有。