---
title: "Android简明开发教程十四：Context Menu 绘制几何图形"
author: "mapdigit"
date: "2011-05-12T17:21:00Z"
tags: []
slug: "android-context-menu"
draft: false
lastmod: "2013-03-22T14:11:20Z"
id: 1834803
url: "https://mapdigit.iteye.com/blog/1834803"
---

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

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

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

&lt;?xml version=”1.0″ encoding=”utf-8″?&gt;  
&lt;menu  
xmlns:android=”[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)“&gt;  
&lt;item  
android:id=”@+id/mnuOval”  
android:title=”Oval”&gt;  
&lt;/item&gt;  
&lt;item  
android:id=”@+id/mnuPear”  
android:title=”Pear”&gt;  
&lt;/item&gt;  
&lt;item  
android:id=”@+id/mnuShape2DDemo”  
android:title=”Shape2D”&gt;  
&lt;/item&gt;  
&lt;/menu&gt;

展开Context Menu，是通过onCreateContextMenu 方法：

1

2

3

4

5

6

7

`@Override`

`public` `void` `onCreateContextMenu(ContextMenu menu, View v,`

`ContextMenuInfo menuInfo) {`

`super.onCreateContextMenu(menu, v, menuInfo);`

`MenuInflater inflater = getMenuInflater();`

`inflater.inflate(R.menu.menu_context_shape, menu);`

`}`

处理Context Menu事件：

1

2

3

4

5

6

7

8

`@Override`

`public` `boolean` `onContextItemSelected(MenuItem item) {`

`menuOption = item.getItemId();`

`drawImage();`

`return` `super.onContextItemSelected(item);`

`}`

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

1

2

3

4

`public` `void` `onCreate(Bundle savedInstanceState) {`

`super.onCreate(savedInstanceState);`

`registerForContextMenu(graphic2dView);`

`}`

[![](http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/04/20110403002.png "20110403002")](http://www.imobilebbs.com/wordpress/wp-content/uploads/2011/04/20110403002.png)

完整代码如下：

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

`public` `class` `Shape extends` `Graphics2DActivity {`

`private` `int` `menuOption;`

`public` `void` `onCreate(Bundle savedInstanceState) {`

`super.onCreate(savedInstanceState);`

`registerForContextMenu(graphic2dView);`

`}`

`@Override`

`protected` `void` `drawImage() {`

`switch` `(menuOption) {`

`case` `R.id.mnuOval:`

`drawOval();`

`break;`

`case` `R.id.mnuPear:`

`drawPear();`

`break;`

`case` `R.id.mnuShape2DDemo:`

`drawShape2D();`

`break;`

`default:`

`drawOval();`

`break;`

`}`

`graphic2dView.refreshCanvas();`

`}`

`@Override`

`public` `void` `onCreateContextMenu(ContextMenu menu, View v,`

`ContextMenuInfo menuInfo) {`

`super.onCreateContextMenu(menu, v, menuInfo);`

`MenuInflater inflater = getMenuInflater();`

`inflater.inflate(R.menu.menu_context_shape, menu);`

`}`

`@Override`

`public` `boolean` `onContextItemSelected(MenuItem item) {`

`menuOption = item.getItemId();`

`drawImage();`

`return` `super.onContextItemSelected(item);`

`}`

`private` `void` `drawOval() {`

`AffineTransform mat1;`

`/** Colors */`

`Color redColor = new` `Color(0x96ff0000, true);`

`Color greenColor = new` `Color(0xff00ff00);`

`mat1 = new` `AffineTransform();`

`mat1.translate(30, 40);`

`mat1.rotate(-30` `* Math.PI / 180.0);`

`// Clear the canvas with white color.`

`graphics2D.clear(Color.WHITE);`

`graphics2D.Reset();`

`graphics2D.setAffineTransform(new` `AffineTransform());`

`SolidBrush brush = new` `SolidBrush(greenColor);`

`graphics2D.fillOval(brush, 20, 60, 100, 50);`

`com.mapdigit.drawing.Pen pen`

`= new` `com.mapdigit.drawing.Pen(redColor, 5);`

`graphics2D.setAffineTransform(mat1);`

`graphics2D.drawOval(pen, 20, 60, 100, 50);`

`}`

`private` `void` `drawPear() {`

`Ellipse circle, oval, leaf, stem;`

`Area circ, ov, leaf1, leaf2, st1, st2;`

`circle = new` `Ellipse();`

`oval = new` `Ellipse();`

`leaf = new` `Ellipse();`

`stem = new` `Ellipse();`

`circ = new` `Area(circle);`

`ov = new` `Area(oval);`

`leaf1 = new` `Area(leaf);`

`leaf2 = new` `Area(leaf);`

`st1 = new` `Area(stem);`

`st2 = new` `Area(stem);`

`graphics2D.clear(Color.WHITE);`

`graphics2D.Reset();`

`int` `w = SharedGraphics2DInstance.CANVAS_WIDTH;`

`int` `h = SharedGraphics2DInstance.CANVAS_HEIGHT;`

`int` `ew = w / 2;`

`int` `eh = h / 2;`

`SolidBrush brush = new` `SolidBrush(Color.GREEN);`

`graphics2D.setDefaultBrush(brush);`

`// Creates the first leaf by filling the`

`//intersection of two Area`

`// objects created from an ellipse.`

`leaf.setFrame(ew - 16, eh - 29, 15, 15);`

`leaf1 = new` `Area(leaf);`

`leaf.setFrame(ew - 14, eh - 47, 30, 30);`

`leaf2 = new` `Area(leaf);`

`leaf1.intersect(leaf2);`

`graphics2D.fill(null, leaf1);`

`// Creates the second leaf.`

`leaf.setFrame(ew + 1, eh - 29, 15, 15);`

`leaf1 = new` `Area(leaf);`

`leaf2.intersect(leaf1);`

`graphics2D.fill(null, leaf2);`

`brush = new` `SolidBrush(Color.BLACK);`

`graphics2D.setDefaultBrush(brush);`

`// Creates the stem by filling the Area`

`//resulting from the subtraction of two`

`//Area objects created from an ellipse.`

`stem.setFrame(ew, eh - 42, 40, 40);`

`st1 = new` `Area(stem);`

`stem.setFrame(ew + 3, eh - 47, 50, 50);`

`st2 = new` `Area(stem);`

`st1.subtract(st2);`

`graphics2D.fill(null, st1);`

`brush = new` `SolidBrush(Color.YELLOW);`

`graphics2D.setDefaultBrush(brush);`

`// Creates the pear itself by filling the`

`//Area resulting from the union of two Area`

`//objects created by two different ellipses.`

`circle.setFrame(ew - 25, eh, 50, 50);`

`oval.setFrame(ew - 19, eh - 20, 40, 70);`

`circ = new` `Area(circle);`

`ov = new` `Area(oval);`

`circ.add(ov);`

`graphics2D.fill(null, circ);`

`}`

`private` `void` `drawShape2D() {`

`Color bg = Color.white;`

`Color fg = Color.black;`

`Color red = Color.red;`

`Color white = Color.white;`

`com.mapdigit.drawing.Pen pen`

`= new` `com.mapdigit.drawing.Pen(fg, 1);`

`SolidBrush brush = new` `SolidBrush(red);`

`// Clear the canvas with white color.`

`graphics2D.clear(bg);`

`graphics2D.Reset();`

`Dimension d = new` `Dimension(SharedGraphics2DInstance.CANVAS_WIDTH,`

`SharedGraphics2DInstance.CANVAS_HEIGHT);`

`int` `gridWidth = d.width / 2;`

`int` `gridHeight = d.height / 6;`

`int` `x = 5;`

`int` `y = 7;`

`int` `rectWidth = gridWidth - 2` `* x;`

`int` `stringY = gridHeight - 3` `- 2` `- 16;`

`int` `rectHeight = stringY - y - 2;`

`graphics2D.draw(pen, new` `Line(x, y + rectHeight - 1,`

`x + rectWidth, y));`

`x += gridWidth;`

`graphics2D.draw(pen, new` `Rectangle(x, y, rectWidth,`

`rectHeight));`

`x += gridWidth;`

`x = 5;`

`y += gridHeight;`

`stringY += gridHeight;`

`graphics2D.draw(pen, new` `RoundRectangle(x, y, rectWidth,`

`rectHeight,`

`10, 10));`

`x += gridWidth;`

`graphics2D.draw(pen, new` `Arc(x, y, rectWidth,`

`rectHeight, 90, 135,`

`Arc.OPEN));`

`x = 5;`

`y += gridHeight;`

`stringY += gridHeight;`

`graphics2D.draw(pen, new` `Ellipse(x, y, rectWidth,`

`rectHeight));`

`x += gridWidth;`

`// draw GeneralPath (polygon)`

`int` `x1Points[] = { x, x + rectWidth, x,`

`x + rectWidth };`

`int` `y1Points[] = { y, y + rectHeight,`

`y + rectHeight, y };`

`com.mapdigit.drawing.geometry.Path polygon`

`= new` `com.mapdigit.drawing.geometry.Path(`

`com.mapdigit.drawing.geometry.Path.WIND_EVEN_ODD,`

`x1Points.length);`

`polygon.moveTo(x1Points[0], y1Points[0]);`

`for` `(int` `index = 1; index < x1Points.length; index++) {`

`polygon.lineTo(x1Points[index], y1Points[index]);`

`}`

`polygon.closePath();`

`graphics2D.draw(pen, polygon);`

`x = 5;`

`y += gridHeight;`

`stringY += gridHeight;`

`int` `x2Points[] = { x, x + rectWidth, x, x + rectWidth };`

`int` `y2Points[] = { y, y + rectHeight, y + rectHeight, y };`

`com.mapdigit.drawing.geometry.Path polyline`

`= new` `com.mapdigit.drawing.geometry.Path(`

`com.mapdigit.drawing.geometry.Path.WIND_EVEN_ODD,`

`x2Points.length);`

`polyline.moveTo(x2Points[0], y2Points[0]);`

`for` `(int` `index = 1; index < x2Points.length; index++) {`

`polyline.lineTo(x2Points[index], y2Points[index]);`

`}`

`graphics2D.draw(pen, polyline);`

`x += gridWidth;`

`graphics2D.setPenAndBrush(pen, brush);`

`graphics2D.fill(null,`

`new` `Rectangle(x, y, rectWidth, rectHeight));`

`graphics2D.draw(null,`

`new` `Rectangle(x, y, rectWidth, rectHeight));`

`x = 5;`

`y += gridHeight;`

`stringY += gridHeight;`

`Color[] colors = new` `Color[] { red, white };`

`int[] fractions = new` `int[] { 0, 255` `};`

`LinearGradientBrush redtowhite`

`= new` `LinearGradientBrush(x, y, x`

`+ rectWidth, y, fractions, colors,`

`com.mapdigit.drawing.Brush.NO_CYCLE);`

`graphics2D.setPenAndBrush(pen, redtowhite);`

`graphics2D.fill(null, new` `RoundRectangle(x, y, rectWidth,`

`rectHeight,`

`10, 10));`

`graphics2D.draw(null, new` `RoundRectangle(x, y, rectWidth,`

`rectHeight,`

`10, 10));`

`x += gridWidth;`

`graphics2D.setPenAndBrush(pen, brush);`

`graphics2D.fill(null, new` `Arc(x, y, rectWidth,`

`rectHeight, 90, 135,`

`Arc.CHORD));`

`graphics2D.draw(null, new` `Arc(x, y, rectWidth,`

`rectHeight, 90, 135,`

`Arc.CHORD));`

`x = 5;`

`y += gridHeight;`

`stringY += gridHeight;`

`int` `x3Points[] = { x, x + rectWidth, x, x + rectWidth };`

`int` `y3Points[] = { y, y + rectHeight, y + rectHeight, y };`

`com.mapdigit.drawing.geometry.Path filledPolygon`

`= new` `com.mapdigit.drawing.geometry.Path(`

`com.mapdigit.drawing.geometry.Path.WIND_EVEN_ODD,`

`x3Points.length);`

`filledPolygon.moveTo(x3Points[0], y3Points[0]);`

`for` `(int` `index = 1; index < x3Points.length; index++) {`

`filledPolygon.lineTo(x3Points[index], y3Points[index]);`

`}`

`filledPolygon.closePath();`

`graphics2D.setPenAndBrush(pen, brush);`

`graphics2D.fill(null, filledPolygon);`

`graphics2D.draw(null, filledPolygon);`

`}`

`}`

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