(X,Y)=(10,20),(50,30),(20,20),(40,22),(45,20),(50,35),........ 。
我想创建一个.Net.Normally我使用Windows窗体的动画。 但是,如果需要的话,我可以使用WPF。
它始于(10,20)point.Actually线从(10,20)开始,然后去(50,30)与10ms的延迟点。 然后,经过10毫秒等从(50,30)到(20,20)。
该值将从一个文本文件中读取。 我可以简单地做两个ArrayList的X&Y,把值从文本file.I只需要知道我怎么能生成这种X动画线,Y从每个节点到另一个10毫秒的延迟?
如果我的问题是很难理解的,请让我知道。 我会尝试,使其更容易。
提前致谢。
如果我理解正确的话,你要因为这将是刚刚绘制的动画就行了。 下面是一个简单的例子,你的价值观。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas Name="lineCanvas" MouseLeftButtonDown="lineCanvas_MouseLeftButtonDown" Background="White" />
</Window>
事件处理程序将在稍后开始的动画。 首先,我们定义数据:
List<Point> Points = new List<Point>();
Storyboard sb;
public MainWindow()
{
InitializeComponent();
Points.Add(new Point(10, 20));
Points.Add(new Point(50, 30));
Points.Add(new Point(20, 20));
Points.Add(new Point(40, 22));
Points.Add(new Point(45, 20));
Points.Add(new Point(50, 35));
InitAnimation();
}
sb
是Storyboard
将包含动画。 Points
可以很容易地充满了从文件中的值。
让我们来初始化动画。 一个新线路将被添加到每个段。 则该行的终点将是动画。
public void InitAnimation()
{
sb = new Storyboard();
for (int i = 0; i < Points.Count - 1; ++i)
{
//new line for current line segment
var l = new Line();
l.Stroke = Brushes.Black;
l.StrokeThickness = 2;
//data from list
var startPoint = Points[i];
var endPoint = Points[i + 1];
//set startpoint = endpoint will result in the line not being drawn
l.X1 = startPoint.X;
l.Y1 = startPoint.Y;
l.X2 = startPoint.X;
l.Y2 = startPoint.Y;
lineCanvas.Children.Add(l);
//Initialize the animations with duration of 1 second for each segment
var daX = new DoubleAnimation(endPoint.X, new Duration(TimeSpan.FromMilliseconds(1000)));
var daY = new DoubleAnimation(endPoint.Y, new Duration(TimeSpan.FromMilliseconds(1000)));
//Define the begin time. This is sum of durations of earlier animations + 10 ms delay for each
daX.BeginTime = TimeSpan.FromMilliseconds(i * 1010);
daY.BeginTime = TimeSpan.FromMilliseconds(i * 1010);
sb.Children.Add(daX);
sb.Children.Add(daY);
//Set the targets for the animations
Storyboard.SetTarget(daX, l);
Storyboard.SetTarget(daY, l);
Storyboard.SetTargetProperty(daX, new PropertyPath(Line.X2Property));
Storyboard.SetTargetProperty(daY, new PropertyPath(Line.Y2Property));
}
}
动画的持续时间可以很容易地根据该线的长度,使它看起来更好而改变。
最后的任务,显示动画:
private void lineCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
sb.Begin(this);
}
下面是关于尼科的伟大回答的变化。 我用PointAnimation
动画的EndPoint
上LineGeometry
其作为Data
的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<Point>()
{
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);
}
}
}