none
怎样以动画的效果画出贝塞尔曲线? RRS feed

  • 问题

  • 我看到了类PointAnimation,可惜它用于绘制直线段。还有类PointAnimationUsingPath,是用于沿着路径移动可视对象。而我只希望像动画的效果画出路径(Path,其中含有贝塞尔曲线等segment)。该如何做呢?
    2013年7月2日 4:02

全部回复

  • 你好,

    这里有篇博客是关于制作贝塞尔曲线动画,虽然是silverlight的,但是你可以参考下:

    http://www.cnblogs.com/Aimeast/archive/2011/09/18/2180474.html


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年7月3日 8:50
    版主
  • Lisa,基于您给出的参考,我写了我的C#代码,调试许久,可是页面上什么也没有发生。郁闷。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Media.Animation;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace MyFirstWpfApplication
    {
        /// <summary>
        /// Interaction logic for Page4bezier.xaml
        /// </summary>
        public partial class Page4bezier : Page
        {
            Storyboard storyboard;

            public Page4bezier()
            {
                InitializeComponent();

                storyboard = new Storyboard();
                InitializeBezierAnimation();
                Loaded += (s, e) => storyboard.Begin();
            }

            private void InitializeBezierAnimation()
            {
                Canvas bezierCanvas = new Canvas();

                Point ptStart = new Point(35, 80);
                Point ptPoint1 = new Point(120, 80);
                Point ptPoint2 = new Point(50, 150);
                Point ptEnding = new Point(135, 150);

                PathGeometry pg = new PathGeometry();
                PathFigure pf = new PathFigure();
                pf.StartPoint = new Point(ptStart.X, ptStart.Y);
                BezierSegment bs = new BezierSegment(new Point(ptStart.X, ptStart.Y), new Point(ptStart.X, ptStart.Y), new Point(ptStart.X, ptStart.Y), true);
                pf.Segments.Add(bs);
                pg.Figures.Add(pf);

                Path path = new Path()
                {
                    Stroke = Brushes.Black,
                    StrokeThickness = 1,
                    Data = pg
                };

                bezierCanvas.Children.Add(path);

                PointAnimation point1Animation = new PointAnimation(new Point(ptStart.X, ptStart.Y), new Point(ptPoint1.X, ptPoint1.Y), new Duration(TimeSpan.FromMilliseconds(1000)));
                PointAnimation point2Animation = new PointAnimation(new Point(ptPoint1.X, ptPoint1.Y), new Point(ptPoint2.X, ptPoint2.Y), new Duration(TimeSpan.FromMilliseconds(1000)));
                PointAnimation point3Animation = new PointAnimation(new Point(ptPoint2.X, ptPoint2.Y), new Point(ptEnding.X, ptEnding.Y), new Duration(TimeSpan.FromMilliseconds(1000)));

                storyboard.Children.Add(point1Animation);
                storyboard.Children.Add(point2Animation);
                storyboard.Children.Add(point3Animation);

                Storyboard.SetTarget(point1Animation, path);
                Storyboard.SetTarget(point2Animation, path);
                Storyboard.SetTarget(point3Animation, path);

                Storyboard.SetTargetProperty(point1Animation, new PropertyPath(BezierSegment.Point1Property));
                Storyboard.SetTargetProperty(point2Animation, new PropertyPath(BezierSegment.Point2Property));
                Storyboard.SetTargetProperty(point3Animation, new PropertyPath(BezierSegment.Point3Property));

                Line ln = new Line();
                ln.X1 = ln.Y1 = 450;
                ln.X2 = ln.Y2 = 340;
                ln.Stroke = Brushes.Blue;

                bezierCanvas.Children.Add(ln);

                this.Content = bezierCanvas;
            }
        }
    }

    2013年7月4日 7:32
  • 怎么没有人帮忙啊,帮我看看这段程序问题到底出在哪里啊?谢谢大神、大家。
    2013年7月11日 2:51