generating animated line

前端 未结 2 1135
说谎
说谎 2021-01-03 05:26

(x,y) =(10,20),(50,30),(20,20),(40,22),(45,20),(50,35),.........

I want to create an animation with .Net.Normally I am using windows form. But if needed, i can use W

2条回答
  •  粉色の甜心
    2021-01-03 06:09

    Here's a variation on Nico's great answer. I use PointAnimation to animate the EndPoint of a LineGeometry which serves as the Data for a Path.

    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace SoGeneratingAnimatedLine
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                var canvas = new Canvas();
    
                Content = canvas;
    
                var points =
                    new List()
                    {
                        new Point(10, 10),
                        new Point(90, 10),
                        new Point(90, 90),
                        new Point(10, 90),
                        new Point(10, 10)
                    };
    
                var sb = new Storyboard();
    
                for (int i = 0; i < points.Count - 1; i++)
                {
                    var lineGeometry = new LineGeometry(points[i], points[i]);
    
                    var path =
                        new Path()
                        {
                            Stroke = Brushes.Black,
                            StrokeThickness = 2,
                            Data = lineGeometry
                        };
    
                    canvas.Children.Add(path);
    
                    var animation =
                        new PointAnimation(points[i], points[i + 1], new Duration(TimeSpan.FromMilliseconds(1000)))
                        {
                            BeginTime = TimeSpan.FromMilliseconds(i * 1010)
                        };
    
                    sb.Children.Add(animation);
    
                    RegisterName("geometry" + i, lineGeometry);
                    Storyboard.SetTargetName(animation, "geometry" + i);
                    Storyboard.SetTargetProperty(animation, new PropertyPath(LineGeometry.EndPointProperty));                
                }
    
                MouseDown += (s, e) => sb.Begin(this);
            }
        }
    }
    

提交回复
热议问题