前面效果中分享了模块对比;今天大周末那就再来分享一下最近实现的效果;基于Y=X^n公式根据不同的系数绘制Gamma曲线效果如下图:
1、曲线的话,咱就用最简单的方式绘制一堆点:
PathFigure pathFigure = new PathFigure(){StartPoint = new Point(0, count),};for(double x = 0; x <= 1; x += 0.001)
{//先计算Y的值double y = Math.Pow(x, coefficient);double finalX = x * count;double finalY = count - (y * count);if(Math.Abs(x - 1) < 0.001){finalX = count;finalY = 0;}var line = new LineSegment(new Point(finalX, finalY), true);pathFigure.Segments.Add(line);
}
2、转换成Path:
PathGeometry pathGeometry = new PathGeometry();
pathGeometry.Figures.Add(pathFigure);
Path path = new Path()
{Fill = new SolidColorBrush(Colors.Transparent),Stroke = new SolidColorBrush(Colors.Red),StrokeThickness = 2,Stretch = Stretch.Uniform,
};
path.Data = pathGeometry;
3、double比较知识点:
public bool DoubleEquals(double value1, double value2)
{//双精度误差var DOUBLE_DELTA = 1E-18;return value1 == value2 || Math.Abs(value1 - value2) < DOUBLE_DELTA;
}
4、Path有了接下来就是动画效果:
Storyboard sb = new Storyboard();
DoubleAnimationUsingKeyFrames topKeyFrames = new DoubleAnimationUsingKeyFrames();
EasingDoubleKeyFrame topKeyFrame = new EasingDoubleKeyFrame();
topKeyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
topKeyFrame.Value = path.StrokeDashOffset;
topKeyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(3));
topKeyFrame.Value = 0;
topKeyFrames.KeyFrames.Add(topKeyFrame);
Storyboard.SetTarget(topKeyFrames, path);
Storyboard.SetTargetProperty(topKeyFrames, new PropertyPath(Shape.StrokeDashOffsetProperty));
sb.Children.Add(topKeyFrames);
sb.Begin();
关于StrokeDashOffset就需要一个通用算法,有需要可以留言和私信我;最终简单的效果先这样吧;以后有时间的话,可以再去摸索一下更复杂的效果;编程不息、Bug不止、无Bug、无生活;改bug的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!如果觉得不错,那就伸出您的小手点个赞并关注一下!