Android开发速成简洁教程十四:Context Menu绘制图形

移动开发 Android
上下文相关菜单(Context Menu)类同PC上按鼠标右键显示的菜单,在Android平台上是长按来激活Context Menu,Context Menu一般用来显示和当前UI内容相关的菜单。

上下文相关菜单(Context Menu)类同PC上按鼠标右键显示的菜单,在Android平台上是长按来激活Context Menu,Context Menu一般用来显示和当前UI内容相关的菜单。

Context Menu的用法和Option Menu非常类似:

首先是创建菜单资源,在res\menu 下新建menu_context_shape.xml,用来显示Oval,Pear,Shape2D:

  1. <?xml version=”1.0″ encoding=”utf-8″?> 
  2. <menu 
  3.   xmlns:android=”http://schemas.android.com/apk/res/android“> 
  4. <item 
  5.    android:id=”@+id/mnuOval” 
  6.    android:title=”Oval”> 
  7. </item> 
  8. <item 
  9.     android:id=”@+id/mnuPear” 
  10.     android:title=”Pear”> 
  11. </item> 
  12. <item 
  13.     android:id=”@+id/mnuShape2DDemo” 
  14.     android:title=”Shape2D”> 
  15. </item> 
  16. </menu> 

展开Context Menu,是通过onCreateContextMenu 方法:

  1. @Override 
  2.     public void onCreateContextMenu(ContextMenu menu, View v, 
  3.       ContextMenuInfo menuInfo) { 
  4.      super.onCreateContextMenu(menu, v, menuInfo); 
  5.      MenuInflater inflater = getMenuInflater(); 
  6.      inflater.inflate(R.menu.menu_context_shape, menu); 
  7.     } 

处理Context Menu事件:

  1. @Override 
  2.     public boolean onContextItemSelected(MenuItem item) { 
  3.      menuOption = item.getItemId(); 
  4.      drawImage(); 
  5.      return super.onContextItemSelected(item);    
  6.     } 

为了在长按时能在View上显示Context Menu,需要为View注册Context Menu:

  1. public void onCreate(Bundle savedInstanceState) { 
  2.      super.onCreate(savedInstanceState); 
  3.      registerForContextMenu(graphic2dView); 
  4.     } 

完整代码如下:

  1. 1   public class Shape extends Graphics2DActivity { 
  2. 2     
  3. 3    private int menuOption; 
  4. 4     
  5. 5    public void onCreate(Bundle savedInstanceState) { 
  6. 6     super.onCreate(savedInstanceState); 
  7. 7     registerForContextMenu(graphic2dView); 
  8. 8    } 
  9. 9     
  10. 10   @Override 
  11. 11   protected void drawImage() { 
  12. 12    switch (menuOption) { 
  13. 13    case R.id.mnuOval: 
  14. 14     drawOval(); 
  15. 15     break
  16. 16    case R.id.mnuPear: 
  17. 17     drawPear(); 
  18. 18     break
  19. 19    case R.id.mnuShape2DDemo: 
  20. 20     drawShape2D(); 
  21. 21     break
  22. 22    default
  23. 23     drawOval(); 
  24. 24     break
  25. 25    } 
  26. 26    graphic2dView.refreshCanvas(); 
  27. 27    
  28. 28   } 
  29. 29    
  30. 30   @Override 
  31. 31   public void onCreateContextMenu(ContextMenu menu, View v, 
  32. 32     ContextMenuInfo menuInfo) { 
  33. 33    super.onCreateContextMenu(menu, v, menuInfo); 
  34. 34    MenuInflater inflater = getMenuInflater(); 
  35. 35    inflater.inflate(R.menu.menu_context_shape, menu); 
  36. 36   } 
  37. 37    
  38. 38   @Override 
  39. 39   public boolean onContextItemSelected(MenuItem item) { 
  40. 40    
  41. 41    menuOption = item.getItemId(); 
  42. 42    drawImage(); 
  43. 43    return super.onContextItemSelected(item); 
  44. 44    
  45. 45   } 
  46. 46    
  47. 47   private void drawOval() { 
  48. 48    AffineTransform mat1; 
  49. 49    
  50. 50    /** Colors */ 
  51. 51    Color redColor = new Color(0x96ff0000true); 
  52. 52    Color greenColor = new Color(0xff00ff00); 
  53. 53    mat1 = new AffineTransform(); 
  54. 54    mat1.translate(3040); 
  55. 55    mat1.rotate(-30 * Math.PI / 180.0); 
  56. 56    // Clear the canvas with white color. 
  57. 57    graphics2D.clear(Color.WHITE); 
  58. 58    graphics2D.Reset(); 
  59. 59    
  60. 60    graphics2D.setAffineTransform(new AffineTransform()); 
  61. 61    SolidBrush brush = new SolidBrush(greenColor); 
  62. 62    graphics2D.fillOval(brush, 206010050); 
  63. 63    
  64. 64    com.mapdigit.drawing.Pen pen 
  65. 65       = new com.mapdigit.drawing.Pen(redColor, 5); 
  66. 66    graphics2D.setAffineTransform(mat1); 
  67. 67    graphics2D.drawOval(pen, 206010050); 
  68. 68   } 
  69. 69    
  70. 70   private void drawPear() { 
  71. 71    Ellipse circle, oval, leaf, stem; 
  72. 72    Area circ, ov, leaf1, leaf2, st1, st2; 
  73. 73    circle = new Ellipse(); 
  74. 74    oval = new Ellipse(); 
  75. 75    leaf = new Ellipse(); 
  76. 76    stem = new Ellipse(); 
  77. 77    circ = new Area(circle); 
  78. 78    ov = new Area(oval); 
  79. 79    leaf1 = new Area(leaf); 
  80. 80    leaf2 = new Area(leaf); 
  81. 81    st1 = new Area(stem); 
  82. 82    st2 = new Area(stem); 
  83. 83    graphics2D.clear(Color.WHITE); 
  84. 84    graphics2D.Reset(); 
  85. 85    int w = SharedGraphics2DInstance.CANVAS_WIDTH; 
  86. 86    int h = SharedGraphics2DInstance.CANVAS_HEIGHT; 
  87. 87    int ew = w / 2
  88. 88    int eh = h / 2
  89. 89    SolidBrush brush = new SolidBrush(Color.GREEN); 
  90. 90    graphics2D.setDefaultBrush(brush); 
  91. 91    // Creates the first leaf by filling the 
  92. 92    //intersection of two Area 
  93. 93    // objects created from an ellipse. 
  94. 94    leaf.setFrame(ew - 16, eh - 291515); 
  95. 95    leaf1 = new Area(leaf); 
  96. 96    leaf.setFrame(ew - 14, eh - 473030); 
  97. 97    leaf2 = new Area(leaf); 
  98. 98    leaf1.intersect(leaf2); 
  99. 99    graphics2D.fill(null, leaf1); 
  100. 100   
  101. 101   // Creates the second leaf. 
  102. 102   leaf.setFrame(ew + 1, eh - 291515); 
  103. 103   leaf1 = new Area(leaf); 
  104. 104   leaf2.intersect(leaf1); 
  105. 105   graphics2D.fill(null, leaf2); 
  106. 106   
  107. 107   brush = new SolidBrush(Color.BLACK); 
  108. 108   graphics2D.setDefaultBrush(brush); 
  109. 109   
  110. 110   // Creates the stem by filling the Area 
  111. 111   //resulting from the subtraction of two 
  112. 112   //Area objects created from an ellipse. 
  113. 113   stem.setFrame(ew, eh - 424040); 
  114. 114   st1 = new Area(stem); 
  115. 115   stem.setFrame(ew + 3, eh - 475050); 
  116. 116   st2 = new Area(stem); 
  117. 117   st1.subtract(st2); 
  118. 118   graphics2D.fill(null, st1); 
  119. 119   
  120. 120   brush = new SolidBrush(Color.YELLOW); 
  121. 121   graphics2D.setDefaultBrush(brush); 
  122. 122   
  123. 123   // Creates the pear itself by filling the 
  124. 124   //Area resulting from the union of two Area 
  125. 125   //objects created by two different ellipses. 
  126. 126   circle.setFrame(ew - 25, eh, 5050); 
  127. 127   oval.setFrame(ew - 19, eh - 204070); 
  128. 128   circ = new Area(circle); 
  129. 129   ov = new Area(oval); 
  130. 130   circ.add(ov); 
  131. 131   graphics2D.fill(null, circ); 
  132. 132  } 
  133. 133   
  134. 134  private void drawShape2D() { 
  135. 135   Color bg = Color.white; 
  136. 136   Color fg = Color.black; 
  137. 137   Color red = Color.red; 
  138. 138   Color white = Color.white; 
  139. 139   com.mapdigit.drawing.Pen pen 
  140. 140      = new com.mapdigit.drawing.Pen(fg, 1); 
  141. 141   SolidBrush brush = new SolidBrush(red); 
  142. 142   // Clear the canvas with white color. 
  143. 143   graphics2D.clear(bg); 
  144. 144   graphics2D.Reset(); 
  145. 145   Dimension d = new Dimension(SharedGraphics2DInstance.CANVAS_WIDTH, 
  146. 146     SharedGraphics2DInstance.CANVAS_HEIGHT); 
  147. 147   int gridWidth = d.width / 2
  148. 148   int gridHeight = d.height / 6
  149. 149   
  150. 150   int x = 5
  151. 151   int y = 7
  152. 152   int rectWidth = gridWidth - 2 * x; 
  153. 153   int stringY = gridHeight - 3 - 2 - 16
  154. 154   int rectHeight = stringY - y - 2
  155. 155   graphics2D.draw(pen, new Line(x, y + rectHeight - 1
  156. 156     x + rectWidth, y)); 
  157. 157   x += gridWidth; 
  158. 158   graphics2D.draw(pen, new Rectangle(x, y, rectWidth, 
  159. 159     rectHeight)); 
  160. 160   x += gridWidth; 
  161. 161   x = 5
  162. 162   y += gridHeight; 
  163. 163   stringY += gridHeight; 
  164. 164   graphics2D.draw(pen, new RoundRectangle(x, y, rectWidth, 
  165. 165     rectHeight, 
  166. 166     1010)); 
  167. 167   x += gridWidth; 
  168. 168   graphics2D.draw(pen, new Arc(x, y, rectWidth, 
  169. 169     rectHeight, 90135
  170. 170     Arc.OPEN)); 
  171. 171   x = 5
  172. 172   y += gridHeight; 
  173. 173   stringY += gridHeight; 
  174. 174   graphics2D.draw(pen, new Ellipse(x, y, rectWidth, 
  175. 175     rectHeight)); 
  176. 176   x += gridWidth; 
  177. 177   // draw GeneralPath (polygon) 
  178. 178   int x1Points[] = { x, x + rectWidth, x, 
  179. 179     x + rectWidth }; 
  180. 180   int y1Points[] = { y, y + rectHeight, 
  181. 181     y + rectHeight, y }; 
  182. 182   com.mapdigit.drawing.geometry.Path polygon 
  183. 183     = new com.mapdigit.drawing.geometry.Path( 
  184. 184     com.mapdigit.drawing.geometry.Path.WIND_EVEN_ODD, 
  185. 185     x1Points.length); 
  186. 186   polygon.moveTo(x1Points[0], y1Points[0]); 
  187. 187   for (int index = 1; index < x1Points.length; index++) { 
  188. 188    polygon.lineTo(x1Points[index], y1Points[index]); 
  189. 189   } 
  190. 190   polygon.closePath(); 
  191. 191   graphics2D.draw(pen, polygon); 
  192. 192   x = 5
  193. 193   y += gridHeight; 
  194. 194   stringY += gridHeight; 
  195. 195   int x2Points[] = { x, x + rectWidth, x, x + rectWidth }; 
  196. 196   int y2Points[] = { y, y + rectHeight, y + rectHeight, y }; 
  197. 197   com.mapdigit.drawing.geometry.Path polyline 
  198. 198     = new com.mapdigit.drawing.geometry.Path( 
  199. 199     com.mapdigit.drawing.geometry.Path.WIND_EVEN_ODD, 
  200. 200     x2Points.length); 
  201. 201   polyline.moveTo(x2Points[0], y2Points[0]); 
  202. 202   for (int index = 1; index < x2Points.length; index++) { 
  203. 203    polyline.lineTo(x2Points[index], y2Points[index]); 
  204. 204   } 
  205. 205   graphics2D.draw(pen, polyline); 
  206. 206   x += gridWidth; 
  207. 207   graphics2D.setPenAndBrush(pen, brush); 
  208. 208   graphics2D.fill(null
  209. 209     new Rectangle(x, y, rectWidth, rectHeight)); 
  210. 210   graphics2D.draw(null
  211. 211     new Rectangle(x, y, rectWidth, rectHeight)); 
  212. 212   x = 5
  213. 213   y += gridHeight; 
  214. 214   stringY += gridHeight; 
  215. 215   Color[] colors = new Color[] { red, white }; 
  216. 216   int[] fractions = new int[] { 0255 }; 
  217. 217   LinearGradientBrush redtowhite 
  218. 218       = new LinearGradientBrush(x, y, x 
  219. 219     + rectWidth, y, fractions, colors, 
  220. 220     com.mapdigit.drawing.Brush.NO_CYCLE); 
  221. 221   graphics2D.setPenAndBrush(pen, redtowhite); 
  222. 222   graphics2D.fill(nullnew RoundRectangle(x, y, rectWidth, 
  223. 223     rectHeight, 
  224. 224     1010)); 
  225. 225   graphics2D.draw(nullnew RoundRectangle(x, y, rectWidth, 
  226. 226     rectHeight, 
  227. 227     1010)); 
  228. 228   x += gridWidth; 
  229. 229   graphics2D.setPenAndBrush(pen, brush); 
  230. 230   graphics2D.fill(nullnew Arc(x, y, rectWidth, 
  231. 231     rectHeight, 90135
  232. 232     Arc.CHORD)); 
  233. 233   graphics2D.draw(nullnew Arc(x, y, rectWidth, 
  234. 234     rectHeight, 90135
  235. 235     Arc.CHORD)); 
  236. 236   x = 5
  237. 237   y += gridHeight; 
  238. 238   stringY += gridHeight; 
  239. 239   int x3Points[] = { x, x + rectWidth, x, x + rectWidth }; 
  240. 240   int y3Points[] = { y, y + rectHeight, y + rectHeight, y }; 
  241. 241   com.mapdigit.drawing.geometry.Path filledPolygon 
  242. 242   = new com.mapdigit.drawing.geometry.Path( 
  243. 243     com.mapdigit.drawing.geometry.Path.WIND_EVEN_ODD, 
  244. 244     x3Points.length); 
  245. 245   filledPolygon.moveTo(x3Points[0], y3Points[0]); 
  246. 246   for (int index = 1; index < x3Points.length; index++) { 
  247. 247    filledPolygon.lineTo(x3Points[index], y3Points[index]); 
  248. 248   } 
  249. 249   filledPolygon.closePath(); 
  250. 250   graphics2D.setPenAndBrush(pen, brush); 
  251. 251   graphics2D.fill(null, filledPolygon); 
  252. 252   graphics2D.draw(null, filledPolygon); 
  253. 253   
  254. 254  } 
  255. 255 }

菜单除了这里介绍的功能外,Android也支持动态菜单或动态修改菜单。具体可以参见Android 文档。

责任编辑:闫佳明 来源: imobilebbs
相关推荐

2013-12-27 12:51:44

Android开发Android应用引路蜂

2023-09-07 08:24:35

图形编辑器开发绘制图形工具

2013-12-04 16:07:27

Android游戏引擎libgdx教程

2013-12-26 16:33:24

Android开发Android应用引路蜂二维图形绘制

2013-12-27 13:27:05

Android开发Android应用RadioButton

2013-12-26 15:43:07

Android开发Android应用Activities

2009-10-23 16:43:01

VB.NET绘制图形

2011-06-30 15:09:37

QT 绘制 图形

2013-12-26 15:10:08

Android开发应用和框架Linux 内核

2013-12-27 12:42:15

Android开发Android应用引路蜂

2013-12-26 15:18:09

Android开发安装开发环境

2013-12-27 15:11:17

Android开发访问Internet绘制在线地图

2013-12-27 14:34:46

Android开发Android应用短信触发示例

2013-12-27 14:16:43

Android开发Android应用线程

2013-12-27 14:05:22

Android开发Android应用Dialog

2013-12-26 15:34:19

Android开发Android应用基本概念

2013-12-27 13:49:22

Android开发Android应用Button

2013-12-26 16:59:12

Android开发Android应用数据绑定Data Bi

2013-12-26 16:24:13

Android开发Android应用Intents

2013-12-27 16:06:10

Android开发Android应用发布应用
点赞
收藏

51CTO技术栈公众号