类Path为一条几何路径,可以通过直线,二次曲线,三次曲线来构成任意路径。Path提供了moveto,lineto,curveto,curvetoCubic,closePath方法来构成路径。
下面的例子动态显示一条BezIEr曲线。
/*** The animation thread.*/Thread _thread;bool _drawn;/** * The random number generator. */Readonly Random _random = new Random();/** * The animated path */Readonly Path _path = new Path();/** * Red brush used to fill the path. */Readonly SolIDBrush _brush = new SolIDBrush(color.Red);const int Numpts = 6;Readonly int[] _animpts = new int[Numpts * 2];Readonly int[] _deltas = new int[Numpts * 2]; private voID BezIErs(){ reset(screenWIDth,screenHeight); _thread = new Thread(RunBezIErs); _thread.Start();} private voID RunBezIErs(){ Thread me = Thread.CurrentThread; if (!_drawn) { lock (this) { graphics2D.Clear(color.White); graphics2D.Fill(_brush,_path); _drawn = true; } } while (_thread == me) { DrawDemo(screenWIDth,screenHeight); }} /*** Generates new points for the path.*/private voID Animate(int[] pts,int[] deltas,int i,int limit){ int newpt = pts[i] + deltas[i]; if (newpt <= 0) { newpt = -newpt; deltas[i] = (_random.Next() & 0x00000003) + 2; } else if (newpt >= limit) { newpt = 2 * limit - newpt; deltas[i] = -((_random.Next() & 0x00000003) + 2); } pts[i] = newpt;} /*** resets the animation data.*/private voID reset(int w,int h){ for (int i = 0; i < _animpts.Length; i += 2) { _animpts[i + 0] = (_random.Next() & 0x00000003) * w / 2; _animpts[i + 1] = (_random.Next() & 0x00000003) * h / 2; _deltas[i + 0] = (_random.Next() & 0x00000003) * 6 + 4; _deltas[i + 1] = (_random.Next() & 0x00000003) * 6 + 4; if (_animpts[i + 0] > w / 2) { _deltas[i + 0] = -_deltas[i + 0]; } if (_animpts[i + 1] > h / 2) { _deltas[i + 1] = -_deltas[i + 1]; } }} /** * Sets the points of the path and draws and fills the path. */private voID DrawDemo(int w,int h){ for (int i = 0; i < _animpts.Length; i += 2) { Animate(_animpts,_deltas,i + 0,w); Animate(_animpts,i + 1,h); } //Generates the new pata data. _path.reset(); int[] ctrlpts = _animpts; int len = ctrlpts.Length; int prevx = ctrlpts[len - 2]; int prevy = ctrlpts[len - 1]; int curx = ctrlpts[0]; int cury = ctrlpts[1]; int mIDx = (curx + prevx) / 2; int mIDy = (cury + prevy) / 2; _path.Moveto(mIDx,mIDy); for (int i = 2; i <= ctrlpts.Length; i += 2) { int x1 = (curx + mIDx) / 2; int y1 = (cury + mIDy) / 2; prevx = curx; prevy = cury; if (i < ctrlpts.Length) { curx = ctrlpts[i + 0]; cury = ctrlpts[i + 1]; } else { curx = ctrlpts[0]; cury = ctrlpts[1]; } mIDx = (curx + prevx) / 2; mIDy = (cury + prevy) / 2; int x2 = (prevx + mIDx) / 2; int y2 = (prevy + mIDy) / 2; _path.Curveto(x1,y1,x2,y2,mIDx,mIDy); } _path.ClosePath(); // clear the clipRect area before production graphics2D.Clear(color.White); graphics2D.Fill(_brush,_path); RefreshBitmap(); }
其中实时更新图像需要CompositionTarget.Rendering事件来处理。CompositionTarget.Rendering += CompositionTargetRendering;
private voID CompositionTargetRendering(object sender,EventArgs e){ bmp.InvalIDate();}
以上是内存溢出为你收集整理的Silverlight 引路蜂二维图形库示例:动画Beziers曲线全部内容,希望文章能够帮你解决Silverlight 引路蜂二维图形库示例:动画Beziers曲线所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)