基本信息
源码名称:WPF 多路径动画描绘轨迹生成几何图形动画
源码大小:0.23M
文件格式:.rar
开发语言:C#
更新时间:2020-04-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 5 元 
   源码介绍

按照轨迹描绘做出几何图像。参照此类方式可制作多种动态数学几何图形。

代码均为后台代码,可加强对path、storyboard等知识的学习理解。

代码可以优化,如有错误自行修正。



public void Draw_Init(Canvas obj)
        {
            Canvas box_view = new Canvas()
            {
                Width = 800,
                Height = 600,
            };
            Canvas.SetLeft(box_view, 100);
            Canvas.SetTop(box_view,50);
            obj.Children.Add(box_view);

            Draw_Line(box_view, new Point(100, 300), new Point(700, 300), Colors.LightSlateGray, 1);
            Draw_Line(box_view, new Point(400, 0), new Point(400,600), Colors.LightSlateGray, 1);

            for (int i = 0; i < 7; i  )
            {
                Draw_Line(obj, new Point(200   i * 100, 350 - 5), new Point(200   i * 100, 350   5), Colors.Black, 1);
                Draw_Line(obj, new Point(495, 50 i * 100), new Point(505, 50 i * 100), Colors.Black, 1);

                string nx = (i-3) != 0 ? (i-3)   "x" : "0";
                string ny = (3-i) != 0 ? (3-i)   "y" : "";

                TextBlock num_x = new TextBlock()
                {
                    FontSize = 12,
                    FontWeight = FontWeights.Bold,
                    FontStyle = FontStyles.Oblique,
                    Text = nx,
                    Opacity = 0.2
                };
                Canvas.SetLeft(num_x, 195   i * 100);
                Canvas.SetTop(num_x,355);
                obj.Children.Add(num_x);

                TextBlock num_y = new TextBlock()
                {
                    FontSize = 12,
                    FontWeight = FontWeights.Bold,
                    FontStyle = FontStyles.Oblique,
                    Text = ny,
                    Opacity = 0.2
                };
                Canvas.SetLeft(num_y, 505);
                Canvas.SetTop(num_y, 45 i*100);
                obj.Children.Add(num_y);
            }
            obj.Children.Add(polyline);

            Path path_border = new Path()
            {
                Stroke = Brushes.LightSlateGray,
                StrokeThickness = 1,
                Data = Geometry.Parse("M 0,0 A 300,300 45 1 1 0,1 Z")
            };
            Canvas.SetLeft(path_border, 100);
            Canvas.SetTop(path_border, 300);
            box_view.Children.Add(path_border);
            Path path_box = new Path()
            {
                StrokeThickness = 1,
                Data = Geometry.Parse("M 0,0 A 200,200 45 1 0 0,1 Z")
            };

            Canvas.SetLeft(path_box, 600);
            Canvas.SetTop(path_box, 300);
            box_view.Children.Add(path_box);

            Canvas move_block = new Canvas()
            {
                Width = 100,
                Height = 100
            };
            Canvas.SetLeft(move_block, 150);
            Canvas.SetTop(move_block, 250);
            box_view.Children.Add(move_block);

            Path arc_centre = new Path()
            {
                Fill = Brushes.Black,
                Data = Geometry.Parse("M 0, 0 A 5, 5 45 1 1 0, 1 Z")
            };
            Canvas.SetLeft(arc_centre, 445);
            Canvas.SetTop(arc_centre,50);
            move_block.Children.Add(arc_centre);

            Path arc_move = new Path()
            {
                Fill = Brushes.Black
            };
            EllipseGeometry arc_elips = new EllipseGeometry()
            {
                Center = new Point(200,50),
                RadiusX = 5,
                RadiusY = 5,
            };
            arc_move.Data = arc_elips;
            Canvas.SetLeft(arc_move, 350);
            Canvas.SetTop(arc_move, 0);
            obj.RegisterName("Arc_Move", arc_elips);
            move_block.Children.Add(arc_move);

            Path path_center = new Path()
            {
                Stroke = Brushes.Blue,
                StrokeThickness = 1,
                Data = Geometry.Parse("M 200,50 A 100,100 45 1 1 200,49 Z")
            };
            Canvas.SetLeft(path_center, 350);
            Canvas.SetTop(path_center,0);
            move_block.Children.Add(path_center);

            translate = new TranslateTransform()
            {
                X = 0,
                Y = 0
            };
            obj.RegisterName("MyTranslateTransform", translate);
            TransformGroup group = new TransformGroup();
            group.Children.Add(translate);

            move_block.RenderTransform = group;
            story_x = new Storyboard();
            story_y = new Storyboard();
            PathGeometry geometry = new PathGeometry() 
            { 
                Figures = PathFigureCollection.Parse("M 0,0 A 200,200 45 1 0 0,1 Z") 
            };
            Set_AnimationPath(geometry, "MyTranslateTransform", "X", PathAnimationSource.X, story_x);
            Set_AnimationPath(geometry, "MyTranslateTransform", "Y", PathAnimationSource.Y, story_y);
            
            line = new LineGeometry() 
            { 
                StartPoint = new Point(100, 50), EndPoint = new Point(200,50) 
            };
            Path path_line = new Path()
            {
                Stroke = Brushes.Black,
                StrokeThickness = 1,
                Data = line
            };
            Canvas.SetLeft(path_line, 350);
            Canvas.SetTop(path_line,0);
            obj.RegisterName("Line_Move", line);
            move_block.Children.Add(path_line);

            story_circle = new Storyboard();
            story_arc = new Storyboard();
            PathGeometry pathGeometry = new PathGeometry() 
            { 
                Figures = PathFigureCollection.Parse("M 200,50 A 100,100 45 1 1 200,49 Z") 
            };
            Set_Locus(obj, pathGeometry, "Line_Move", "EndPoint", story_circle);
            Set_Locus(obj, pathGeometry, "Arc_Move", "Center", story_arc);
        }

        private void Set_Locus(Canvas obj, PathGeometry geo, string targetname, string value, Storyboard storyboard)
        {
            PointAnimationUsingPath pointAnimationUsingPath = new PointAnimationUsingPath()
            {
                Duration = new Duration(TimeSpan.FromSeconds(delay / partition)),
                RepeatBehavior = RepeatBehavior.Forever,
            };
            PathGeometry pathGeometry = geo;
            pointAnimationUsingPath.PathGeometry = pathGeometry;

            Storyboard.SetTargetName(pointAnimationUsingPath, targetname);
            Storyboard.SetTargetProperty(pointAnimationUsingPath, new PropertyPath(value));

            storyboard.Children.Add(pointAnimationUsingPath);
            storyboard.Begin(obj, true);
            storyboard.Pause(obj);
        }

        private void Set_AnimationPath(PathGeometry geo, string targetname, string value, PathAnimationSource source, Storyboard storyboard)
        {
            DoubleAnimationUsingPath usingPath = new DoubleAnimationUsingPath()
            {
                Duration = new Duration(TimeSpan.FromSeconds(delay)),
                Source = source,
            };
            usingPath.PathGeometry = geo;

            Storyboard.SetTargetName(usingPath, targetname);
            Storyboard.SetTargetProperty(usingPath, new PropertyPath(value));

            storyboard.Children.Add(usingPath);
            storyboard.Completed  = Story_Completed;
        }