WPF。图案动画class
WPF. Pattern animation class
[我创建了一个 DrawingLine
动画 class 来绘制图案。
构造函数开始创建和动画线条的过程:
internal DrawingLine(double x, double y, int _thickness, Brush _brush, Canvas _canvas)
在动画结束时,此方法生成一个新行:
void CreateNewLine(object sender, EventArgs e)
{
Line newLine = new Line();
switch (lines.Count % 2 == 0)
{
case true:
{
if (lines.Count < 18)
{
newLine.X1 = 0;
newLine.Y1 = 180 - offset;
newLine.X2 = 0;
newLine.Y2 = 180 - offset;
}
// ... <-- Here is the math determines the positions of the points of the line
}
该方法产生动画:
void AnimateXY()
{
DoubleAnimation lineXAnimation = new DoubleAnimation();
lineXAnimation.From = CurrentLine.X1;
lineXAnimation.To = to_X;
lineXAnimation.Duration = TimeSpan.FromSeconds(duration);
DoubleAnimation lineYAnimation = new DoubleAnimation();
lineYAnimation.From = CurrentLine.Y1;
lineYAnimation.To = to_Y;
lineYAnimation.Duration = TimeSpan.FromSeconds(duration);
lineYAnimation.Completed += CreateNewLine;
CurrentLine.BeginAnimation(Line.X2Property, lineXAnimation);
CurrentLine.BeginAnimation(Line.Y2Property, lineYAnimation);
}
问题:循环此动画的最佳方式是什么,这样就不会出现内存泄漏?我还想听听有关改进此类 classes 结构的一般建议。附上图案图片。]1
我找到了问题的答案。如果class里面全是动画,那么它就支持递归调用(递归创建)。一个递归调用让动画无限循环
[我创建了一个 DrawingLine
动画 class 来绘制图案。
构造函数开始创建和动画线条的过程:
internal DrawingLine(double x, double y, int _thickness, Brush _brush, Canvas _canvas)
在动画结束时,此方法生成一个新行:
void CreateNewLine(object sender, EventArgs e)
{
Line newLine = new Line();
switch (lines.Count % 2 == 0)
{
case true:
{
if (lines.Count < 18)
{
newLine.X1 = 0;
newLine.Y1 = 180 - offset;
newLine.X2 = 0;
newLine.Y2 = 180 - offset;
}
// ... <-- Here is the math determines the positions of the points of the line
}
该方法产生动画:
void AnimateXY()
{
DoubleAnimation lineXAnimation = new DoubleAnimation();
lineXAnimation.From = CurrentLine.X1;
lineXAnimation.To = to_X;
lineXAnimation.Duration = TimeSpan.FromSeconds(duration);
DoubleAnimation lineYAnimation = new DoubleAnimation();
lineYAnimation.From = CurrentLine.Y1;
lineYAnimation.To = to_Y;
lineYAnimation.Duration = TimeSpan.FromSeconds(duration);
lineYAnimation.Completed += CreateNewLine;
CurrentLine.BeginAnimation(Line.X2Property, lineXAnimation);
CurrentLine.BeginAnimation(Line.Y2Property, lineYAnimation);
}
问题:循环此动画的最佳方式是什么,这样就不会出现内存泄漏?我还想听听有关改进此类 classes 结构的一般建议。附上图案图片。]1
我找到了问题的答案。如果class里面全是动画,那么它就支持递归调用(递归创建)。一个递归调用让动画无限循环