Windows Mobile引路蜂地图开发示例:二维图形库

jerry 地图开发 2015年11月26日 收藏

Windows Mobile引路蜂地图开发包带有一个高效二维图形库,这是因为诸如LineCap, LineJoin, Brush, TextBrush, Path 等方法在Windows .Net Compact Framework 平台上不支持。Windows Mobile引路蜂地图开发包中的二维图形库弥补了这些缺陷,它提供了在桌面平台System.Drawing.Drawing2D中相应功能。 

 

引路蜂地图开发包使用了这个图形库来绘制路径以及矢量地图。 

基本知识 

二维图形库使用类Graphics2D作为画板,内部画板实际为一个整数型二维数组。这种设计可以实现平台无关性。在绘制好图形后,最终是要在屏幕上显示的。在Windows Mobile平台上没有提供直接绘制整数型二维数组的方法。下面是在Windows Mobile绘制数型二维数组的方法。数组中每个元素为一个32位整数,格式为 AAAARRRRGGGGBBBB,分别代表透明度,红色,绿色,蓝色。 

  1. /// <summary>
  2. /// Graphics 2D Object
  3. /// </summary>
  4. private readonly Graphics2D graphics2D; 
  5.  
  6. /// <summary>
  7. /// screen width
  8. /// </summary>
  9. private readonly int screenWidth; 
  10.  
  11. /// <summary>
  12. /// screen Height
  13. /// </summary>
  14. private readonly int screenHeight;
  15. .....
  16. screenWidth = Width;
  17. screenHeight = Height;
  18. graphics2D = new Graphics2D(screenWidth, screenHeight);
  1. private void MainForm_Paint(object sender, PaintEventArgs e)
  2. {
  3.     DrawRGB(e.Graphics, graphics2D.Argb, 0, 0, screenWidth, screenHeight);
  4. }
  5.  
  6. ////////////////////////////////////////////////////////////////////////////
  7. //--------------------------------- REVISIONS ------------------------------
  8. // Date       Name                 Tracking #         Description
  9. // ---------  -------------------  -------------      ----------------------
  10. // 24SEP2010  James Shen                               Code review
  11. ////////////////////////////////////////////////////////////////////////////
  12. /// <summary>
  13. /// Draws the RGB.
  14. /// </summary>
  15. /// <param name="graphics">The graphics.</param>
  16. /// <param name="rgbData">The RGB data.</param>
  17. /// <param name="x">The x.</param>
  18. /// <param name="y">The y.</param>
  19. /// <param name="w">The w.</param>
  20. /// <param name="h">The h.</param>
  21. private static void DrawRGB(Graphics graphics, int[] rgbData, int x,
  22.    int y, int w, int h)
  23. {
  24.     Bitmap bmp = new Bitmap(w, h);
  25.     System.Drawing.Rectangle rect =
  26.  new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
  27.     BitmapData bmpData =
  28.     bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
  29.     IntPtr ptr = bmpData.Scan0;
  30.     System.Runtime.InteropServices.Marshal.Copy(rgbData, 0, ptr, rgbData.Length);
  31.     bmp.UnlockBits(bmpData);
  32.     graphics.DrawImage(bmp, x, y);
  33. }

下面简单介绍一下图形库,功能基本和桌面平台类似。

颜色

  1. /**
  2.  * The solid (full opaque) red color in the ARGB space
  3.  */
  4. Color redColor = new Color(0xffff0000, false);
  5.  
  6. /**
  7.  * The semi-opaque green color in the ARGB space (alpha is 0x78)
  8.  */
  9. Color greenColor = new Color(0x7800ff00, true);
  10.  
  11. /**
  12.  * The semi-opaque blue color in the ARGB space (alpha is 0x78)
  13.  */
  14. Color blueColor = new Color(0x780000ff, true);
  15. /**
  16.  * The semi-opaque yellow color in the ARGB space ( alpha is 0x78)
  17.  */
  18. Color yellowColor = new Color(0x78ffff00, true);
  19.  
  20. /**
  21.  * The dash array
  22.  */
  23. int[] dashArray = { 20, 8 };
  24. graphics2D.Reset();
  25. graphics2D.Clear(Color.Black);
  26. SolidBrush brush = new SolidBrush(redColor);
  27. graphics2D.FillOval(brush, 30, 60, 80, 80);
  28. brush = new SolidBrush(greenColor);
  29. graphics2D.FillOval(brush, 60, 30, 80, 80);
  30. Pen pen = new Pen(yellowColor, 10, Pen.CapButt, Pen.JoinMiter, dashArray, 0);
  31. brush = new SolidBrush(blueColor);
  32. graphics2D.SetPenAndBrush(pen, brush);
  33. graphics2D.FillOval(null, 90, 60, 80, 80);
  34. graphics2D.DrawOval(null, 90, 60, 80, 80);
  35. Invalidate();

线段接头(LineCap)

  1. Color blackColor = new Color(0x000000);
  2. Color whiteColor = new Color(0xffffff);
  3. graphics2D.Reset();
  4. graphics2D.Clear(Color.White);
  5.  
  6. Pen pen = new Pen(blackColor, 20, Pen.CapButt, Pen.JoinMiter);
  7. graphics2D.DrawLine(pen, 40, 60, 140, 60);
  8. pen = new Pen(whiteColor, 1);
  9. graphics2D.DrawLine(pen, 40, 60, 140, 60);
  10.  
  11. pen = new Pen(blackColor, 20, Pen.CapRound, Pen.JoinMiter);
  12. graphics2D.DrawLine(pen, 40, 100, 140, 100);
  13. pen = new Pen(whiteColor, 1);
  14. graphics2D.DrawLine(pen, 40, 100, 140, 100);
  15.  
  16. pen = new Pen(blackColor, 20, Pen.CapSquare, Pen.JoinMiter);
  17. graphics2D.DrawLine(pen, 40, 140, 140, 140);
  18. pen = new Pen(whiteColor, 1);
  19. graphics2D.DrawLine(pen, 40, 140, 140, 140);
  20. Invalidate();

梨子

最后一个例子是利用各种几何图形通过“加”,“减”,“并”操作组成一个梨子图形。

  1. Ellipse circle, oval, leaf, stem;
  2. Area circ, ov, leaf1, leaf2, st1, st2;
  3. circle = new Ellipse();
  4. oval = new Ellipse();
  5. leaf = new Ellipse();
  6. stem = new Ellipse();
  7. circ = new Area(circle);
  8. ov = new Area(oval);
  9. leaf1 = new Area(leaf);
  10. leaf2 = new Area(leaf);
  11. st1 = new Area(stem);
  12. st2 = new Area(stem);
  13. graphics2D.Reset();
  14. graphics2D.Clear(Color.White);
  15. int w = screenWidth;
  16. int h = screenHeight;
  17. int ew = w / 2;
  18. int eh = h / 2;
  19. SolidBrush brush = new SolidBrush(Color.Green);
  20. graphics2D.DefaultBrush = brush;
  21. // Creates the first leaf by filling the intersection of two Area
  22. //objects created from an ellipse.
  23. leaf.SetFrame(ew - 16, eh - 29, 15, 15);
  24. leaf1 = new Area(leaf);
  25. leaf.SetFrame(ew - 14, eh - 47, 30, 30);
  26. leaf2 = new Area(leaf);
  27. leaf1.Intersect(leaf2);
  28. graphics2D.Fill(null, leaf1);
  29.  
  30. // Creates the second leaf.
  31. leaf.SetFrame(ew + 1, eh - 29, 15, 15);
  32. leaf1 = new Area(leaf);
  33. leaf2.Intersect(leaf1);
  34. graphics2D.Fill(null, leaf2);
  35.  
  36. brush = new SolidBrush(Color.Black);
  37. graphics2D.DefaultBrush = brush;
  38.  
  39. // Creates the stem by filling the Area resulting from the
  40. //subtraction of two Area objects created from an ellipse.
  41. stem.SetFrame(ew, eh - 42, 40, 40);
  42. st1 = new Area(stem);
  43. stem.SetFrame(ew + 3, eh - 47, 50, 50);
  44. st2 = new Area(stem);
  45. st1.Subtract(st2);
  46. graphics2D.Fill(null, st1);
  47.  
  48. brush = new SolidBrush(Color.Yellow);
  49. graphics2D.DefaultBrush = brush;
  50.  
  51. // Creates the pear itself by filling the Area resulting from the
  52. //union of two Area objects created by two different ellipses.
  53. circle.SetFrame(ew - 25, eh, 50, 50);
  54. oval.SetFrame(ew - 19, eh - 20, 40, 70);
  55. circ = new Area(circle);
  56. ov = new Area(oval);
  57. circ.Add(ov);
  58. graphics2D.Fill(null, circ);
  59. Invalidate();