我一直在尝试使椭圆遵循给定的轨迹与DoubleAnimation类。到目前为止还不太好。
我创建了一个自定义类AnimationQueue:
private class AnimationQueue
{
private List<DoubleAnimation> animation1 ;
private DependencyProperty property1;
private bool running1;
private List<DoubleAnimation> animation2;
private DependencyProperty property2;
private bool running2;
private int curent;
private UIElement element;
public AnimationQueue(UIElement element , DependencyProperty property )
{
curent = -1;
this.element = element;
animation1 = new List<DoubleAnimation>();
animation2 = new List<DoubleAnimation>();
this.property1 = property;
}
public AnimationQueue(UIElement element, DependencyProperty property1 , DependencyProperty property2)
{
curent = -1;
this.element = element;
animation1 = new List<DoubleAnimation>();
animation2 = new List<DoubleAnimation>();
this.property1 = property1;
this.property2 = property2;
}
public void queueAnimation(DoubleAnimation animation1, DoubleAnimation animation2)
{
animation1.Completed += (s, e) =>
{
running1 = false;
};
animation2.Completed += (s, e) =>
{
running2 = false;
};
this.animation1.Add(animation1);
this.animation2.Add(animation2);
}
public void start(int i)
{
if (animation1.Count != animation2.Count)
Console.WriteLine("Animation Queue not equal");
else
{
if (i == animation1.Count)
Console.WriteLine("Animation finished");
else if (i <= animation1.Count && i <= animation2.Count)
{
element.BeginAnimation(property1, animation1[i]);
element.BeginAnimation(property2, animation2[i]);
if (running1 == false && running2 == false)
{
i++;
start(i);
}
}
}
}
public void start()
{
curent = 0;
element.BeginAnimation(property1, animation1[curent]);
element.BeginAnimation(property2, animation2[curent]);
if(running1 == false && running2 == false)
start(curent++);
}
}`
在这个类中,我为坐标列表中的每个点添加2个动画。1表示x轴运动的动画,1表示y轴运动的任意化。这样添加:
public void StartCourse()
{
int i = 0;
List<Point> coordinates = course.firstFuntion();
while (coordinates.Count != i)
{
queue1.queueAnimation(new DoubleAnimation(coordinates[i].X, new Duration(TimeSpan.FromMilliseconds(500))),
new DoubleAnimation(coordinates[i].Y, new Duration(TimeSpan.FromMilliseconds(500)))
);
i++;
}
queue1.start();
}
我的问题是,我相信,在AnimationQueue.Start()方法。程序启动动画,但是它只执行第一个动画。之后就停止了。我试图利用动画。完成事件处理程序,但没有成功。
BeginAnimation异步启动动画。我想这可能会有帮助:
private class AnimationQueue
{
private List<DoubleAnimation> animation1 ;
private DependencyProperty property1;
private bool running1;
private List<DoubleAnimation> animation2;
private DependencyProperty property2;
private bool running2;
private int curent;
private UIElement element;
public AnimationQueue(UIElement element , DependencyProperty property )
{
curent = -1;
this.element = element;
animation1 = new List<DoubleAnimation>();
animation2 = new List<DoubleAnimation>();
this.property1 = property;
}
public AnimationQueue(UIElement element, DependencyProperty property1 , DependencyProperty property2)
{
curent = -1;
this.element = element;
animation1 = new List<DoubleAnimation>();
animation2 = new List<DoubleAnimation>();
this.property1 = property1;
this.property2 = property2;
}
public void queueAnimation(DoubleAnimation animation1, DoubleAnimation animation2)
{
this.animation1.Add(animation1);
this.animation2.Add(animation2);
animation1.Completed += (s, e) =>
{
if (this.animation1.Contains(animation1))
{
int index1 = this.animation1.IndexOf(animation1);
if (index1 + 1 < this.animation1.Count)
{
element.BeginAnimation(property1, animation1[index1 + 1]);
}
}
};
animation2.Completed += (s, e) =>
{
if (this.animation2.Contains(animation2))
{
int index2 = this.animation2.IndexOf(animation2);
if (index2 + 1 < this.animation2.Count)
{
element.BeginAnimation(property2, animation2[index2 + 1]);
}
}
};
}
public void start()
{
curent = 0;
element.BeginAnimation(property1, animation1[curent]);
element.BeginAnimation(property2, animation2[curent]);
}
}`