2014-12-18 58 views
1

我設法在工作中爲一個小型項目清除了一些可行的C#代碼,但是我遇到了一個問題。此代碼將在正在播放的視頻上劃一條線,但我需要對其進行修改,以便第二條線將開始在原始線的末端繪製,以便我可以通過連接線顯示路徑。在C中繪製連接線#

如何在播放的第2秒開始從第一行的末尾開始繪製第二行。

public MainWindow() 
    { 
     InitializeComponent(); 
     Line line = new Line(); 
     canvas1.Children.Add(line); 
     line.Stroke = Brushes.Red; 
     line.StrokeThickness = 2; 
     line.X1 = 100; 
     line.Y1 = 100; 
     Storyboard sb = new Storyboard(); 
     DoubleAnimation da = new DoubleAnimation(line.Y2, 500, new Duration(new TimeSpan(0, 0, 1))); 
     DoubleAnimation da1 = new DoubleAnimation(line.X2, 150, new Duration(new TimeSpan(0, 0, 1))); 
     Storyboard.SetTargetProperty(da, new PropertyPath("(Line.Y2)")); 
     Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X2)")); 
     sb.Children.Add(da); 
     sb.Children.Add(da1); 
     line.BeginStoryboard(sb); 
+0

在以Visibility屬性爲目標的第二行上使用ObjectAnimationUsingKeyFrames。將其設置爲在關鍵幀0處摺疊,並在關鍵幀00:02:00處設爲可見。我看到的第二個解決方案是使用DoubleAnimationUsingKeyFrames設置不透明度屬性 – nkoniishvt

+0

在第二秒無法簡單地創建新行? – caine1337

+0

如果存在我不知道這個方法。故事板是爲動畫屬性製作的,而不是創建任何東西。所以我猜想你不能。 – nkoniishvt

回答

0

這是我想出了: 只需創建點的名單,並調用這些方法。它將從一點到另一點畫線。

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.Navigation; 
using System.Windows.Shapes; 
using System.Windows.Media.Animation; 

namespace WpfApplication4 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     List<Point> points = null; 
     List<Line> lines = null; 
     List<Storyboard> boards = null; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      points = new List<Point>(); 
      lines = new List<Line>(); 
      boards = new List<Storyboard>(); 

      points.Add(new Point(0, 0)); 
      points.Add(new Point(200, 200)); 
      points.Add(new Point(200, 0)); 
      points.Add(new Point(0, 0)); 
      points.Add(new Point(0, 200)); 
      points.Add(new Point(200, 200)); 
      points.Add(new Point(300, 200)); 

      SetupLines(); 
      DrawLines(); 
     } 

     public void SetupLines() 
     { 
      int speed = 1; 
      lines.Add(new Line()); 
      boards.Add(new Storyboard()); 

      for (int i = 0; i < points.Count - 1; i++) 
      { 
       lines.Add(new Line()); 
       boards.Add(new Storyboard()); 

       canvas1.Children.Add(lines[i]); 
       lines[i].Stroke = Brushes.Red; 
       lines[i].StrokeThickness = 7; 

       lines[i].X1 = points[i].X; 
       lines[i].Y1 = points[i].Y; 
       lines[i].X2 = points[i].X; 
       lines[i].Y2 = points[i].Y; 

       DoubleAnimation da = new DoubleAnimation(points[i].X, points[i+1].X, new Duration(new TimeSpan(0, 0, speed))); 
       DoubleAnimation da1 = new DoubleAnimation(points[i].Y, points[i+1].Y, new Duration(new TimeSpan(0, 0, speed))); 

       Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)")); 
       Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.Y2)")); 

       boards[i].Children.Add(da); 
       boards[i].Children.Add(da1); 

       da.BeginTime = new TimeSpan(0, 0, i * speed); 
       da1.BeginTime = new TimeSpan(0, 0, i * speed); 

       lines.Add(new Line()); 
       boards.Add(new Storyboard()); 
      } 
     } 

     public void DrawLines() 
     { 
      for (int i = 0; i < boards.Count - 1; i++) 
      { 
       lines[i].BeginStoryboard(boards[i]); 
      } 
     } 
    } 
}