引路蜂二维图形画笔(Pen)示例含有四个示例,Lines ,Dashes ,LineJoin 和LineCap。打算采用Option Menu(主菜单)的方式来选择不同示例。
首先要对GuidebeeGraphics2DView,和Graphics2DActivity做些改动,从这个示例开始,GuidebeeGraphics2DView需要动态绘制不同图形(可以通过菜单,或是Thread)。在GuidebeeGraphics2DView增加下面两个方法:
1
2
3
4
5
6
7
8
9
|
final
Runnable updateCanvas =
new
Runnable() {
public
void
run() {
invalidate();
}
};
public
void
refreshCanvas(){
post(updateCanvas);
}
|
post可以用在非UI Thread中Call UI Thread中方法。这里只是触发屏幕重绘事件以刷新屏幕显示。
在Graphics2DActivity增加一个变量protected GuidebeeGraphics2DView graphic2dView; 来获得对应的GuidebeeGraphics2DView实例。
graphic2dView=(GuidebeeGraphics2DView)findViewById(R.id.graphics2dview);
使用Option Menu,尽管也可以完全使用代码来创建菜单,更一般的方法是使用菜单资源。在res 下创建menu 子目录,然后在res/menu下创建menu_option_line.xml 用来显示Pen的四个示例选项:
<?xml version=”1.0″ encoding=”utf-8″?>
<menu
xmlns:android=”http://schemas.android.com/apk/res/android“>
<item
android:id=”@+id/mnuLines”
android:title=”Lines”
android:icon=”@drawable/icon1″>
</item>
<item
android:id=”@+id/mnuDashes”
android:title=”Dashes”
android:icon=”@drawable/icon2″>
</item>
<item
android:id=”@+id/mnuLineCap”
android:title=”LineCap”
android:icon=”@drawable/icon3″>
</item>
<item
android:id=”@+id/mnuLineJoin”
android:title=”LineJoin”
android:icon=”@drawable/icon4″>
</item>
</menu>
<menu></menu> 定义菜单,<item></item>定义菜单项,<item></item>可以嵌套以定义子菜单。菜单可以定义id, Icon,Text等属性。也可以支持单选,多选,此时就需要借助<group></group>,使用group可以把一最菜单项定义为一个组,可以使用setGroupVisible()来显示隐藏整个菜单组,Enable 或是Disable整个菜单组setGroupEnabled()等。最关键的,有了组才能实现菜单的单选和多选功能:
<?xml version=”1.0″ encoding=”utf-8″?>
<menu xmlns:android=”http://schemas.android.com/apk/res/android“>
<item android:id=”@+id/item1″
android:icon=”@drawable/item1″
android:title=”@string/item1″ />
<!– menu group –>
<group android:id=”@+id/group1″>
<item android:id=”@+id/groupItem1″
android:title=”@string/groupItem1″ />
<item android:id=”@+id/groupItem2″
android:title=”@string/groupItem2″ />
</group>
</menu>
定义好菜单资源后,就可以使用MenuInflater.inflate()展开菜单,一般需要在Activity的onCreateOptionsMenu()展开菜单:
1
2
3
4
5
6
|
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_option_line, menu);
return
true
;
}
|
最后是响应菜单事件:
1
2
3
4
5
6
|
@Override
public
boolean
onOptionsItemSelected(MenuItem item) {
menuOption = item.getItemId();
drawImage();
return
true
;
}
|
item.getItemId(); 返回菜单的ID(在菜单资源中定义)。
完整代码如下:
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
|
public
class
Pen
extends
Graphics2DActivity {
int
menuOption;
@Override
protected
void
drawImage() {
switch
(menuOption) {
case
R.id.mnuLines:
drawLines();
break
;
case
R.id.mnuDashes:
drawDash();
break
;
case
R.id.mnuLineCap:
drawLineCap();
break
;
case
R.id.mnuLineJoin:
drawLineJoin();
break
;
default
:
drawLines();
break
;
}
graphic2dView.refreshCanvas();
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_option_line, menu);
return
true
;
}
@Override
public
boolean
onOptionsItemSelected(MenuItem item) {
menuOption = item.getItemId();
drawImage();
return
true
;
}
private
void
drawLineJoin() {
Color blackColor =
new
Color(
0xff000000
);
Color whiteColor =
new
Color(
0xffffffff
);
com.mapdigit.drawing.geometry.Path path
=
new
com.mapdigit.drawing.geometry.Path();
path.moveTo(
40
,
60
);
path.lineTo(
90
,
20
);
path.lineTo(
140
,
60
);
// Clear the canvas with white color.
graphics2D.clear(Color.WHITE);
AffineTransform matrix =
new
AffineTransform();
graphics2D.setAffineTransform(matrix);
com.mapdigit.drawing.Pen pen
=
new
com.mapdigit.drawing.Pen(blackColor,
20
, com.mapdigit.drawing.Pen.CAP_BUTT,
com.mapdigit.drawing.Pen.JOIN_MITER);
graphics2D.draw(pen, path);
pen =
new
com.mapdigit.drawing.Pen(whiteColor,
1
);
graphics2D.draw(pen, path);
matrix.translate(
0
,
50
);
graphics2D.setAffineTransform(matrix);
pen =
new
com.mapdigit.drawing.Pen(blackColor,
20
,
com.mapdigit.drawing.Pen.CAP_BUTT,
com.mapdigit.drawing.Pen.JOIN_ROUND);
graphics2D.draw(pen, path);
pen =
new
com.mapdigit.drawing.Pen(whiteColor,
1
);
graphics2D.draw(pen, path);
matrix =
new
AffineTransform();
matrix.translate(
0
,
100
);
graphics2D.setAffineTransform(matrix);
pen =
new
com.mapdigit.drawing.Pen(blackColor,
20
,
com.mapdigit.drawing.Pen.CAP_BUTT,
com.mapdigit.drawing.Pen.JOIN_BEVEL);
graphics2D.draw(pen, path);
pen =
new
com.mapdigit.drawing.Pen(whiteColor,
1
);
graphics2D.draw(pen, path);
}
private
void
drawLineCap() {
Color blackColor =
new
Color(
0xff000000
);
Color whiteColor =
new
Color(
0xffffffff
);
// Clear the canvas with white color.
graphics2D.clear(Color.WHITE);
AffineTransform matrix =
new
AffineTransform();
graphics2D.setAffineTransform(matrix);
com.mapdigit.drawing.Pen pen
=
new
com.mapdigit.drawing.Pen(blackColor,
20
, com.mapdigit.drawing.Pen.CAP_BUTT,
com.mapdigit.drawing.Pen.JOIN_MITER);
graphics2D.drawLine(pen,
40
,
60
,
140
,
60
);
pen =
new
com.mapdigit.drawing.Pen(whiteColor,
1
);
graphics2D.drawLine(pen,
40
,
60
,
140
,
60
);
pen =
new
com.mapdigit.drawing.Pen(blackColor,
20
,
com.mapdigit.drawing.Pen.CAP_ROUND,
com.mapdigit.drawing.Pen.JOIN_MITER);
graphics2D.drawLine(pen,
40
,
100
,
140
,
100
);
pen =
new
com.mapdigit.drawing.Pen(whiteColor,
1
);
graphics2D.drawLine(pen,
40
,
100
,
140
,
100
);
pen =
new
com.mapdigit.drawing.Pen(blackColor,
20
,
com.mapdigit.drawing.Pen.CAP_SQUARE,
com.mapdigit.drawing.Pen.JOIN_MITER);
graphics2D.drawLine(pen,
40
,
140
,
140
,
140
);
pen =
new
com.mapdigit.drawing.Pen(whiteColor,
1
);
graphics2D.drawLine(pen,
40
,
140
,
140
,
140
);
}
private
void
drawLines() {
Color greenColor =
new
Color(
0xff00ff00
);
// Clear the canvas with white color.
graphics2D.clear(Color.WHITE);
AffineTransform matrix =
new
AffineTransform();
graphics2D.setAffineTransform(matrix);
com.mapdigit.drawing.Pen pen
=
new
com.mapdigit.drawing.Pen(greenColor,
1
);
graphics2D.drawLine(pen,
20
,
150
,
60
,
50
);
pen =
new
com.mapdigit.drawing.Pen(greenColor,
2
);
graphics2D.drawLine(pen,
40
,
150
,
80
,
50
);
pen =
new
com.mapdigit.drawing.Pen(greenColor,
3
);
graphics2D.drawLine(pen,
60
,
150
,
100
,
50
);
pen =
new
com.mapdigit.drawing.Pen(greenColor,
5
);
graphics2D.drawLine(pen,
80
,
150
,
120
,
50
);
pen =
new
com.mapdigit.drawing.Pen(greenColor,
7
);
graphics2D.drawLine(pen,
100
,
150
,
140
,
50
);
pen =
new
com.mapdigit.drawing.Pen(greenColor,
10
);
graphics2D.drawLine(pen,
120
,
150
,
160
,
50
);
}
private
void
drawDash() {
Color blackColor =
new
Color(
0xff000000
);
int
dashArray1[] = {
2
,
2
};
int
dashArray2[] = {
6
,
6
};
int
dashArray3[] = {
4
,
1
,
2
,
1
,
1
,
6
};
// Clear the canvas with white color.
graphics2D.clear(Color.WHITE);
AffineTransform matrix =
new
AffineTransform();
graphics2D.setAffineTransform(matrix);
com.mapdigit.drawing.Pen pen
=
new
com.mapdigit.drawing.Pen(blackColor,
20
, com.mapdigit.drawing.Pen.CAP_BUTT,
com.mapdigit.drawing.Pen.JOIN_MITER, dashArray1,
0
);
graphics2D.drawLine(pen,
40
,
60
,
140
,
60
);
pen =
new
com.mapdigit.drawing.Pen(blackColor,
20
,
com.mapdigit.drawing.Pen.CAP_BUTT,
com.mapdigit.drawing.Pen.JOIN_MITER, dashArray2,
0
);
graphics2D.drawLine(pen,
40
,
100
,
140
,
100
);
pen =
new
com.mapdigit.drawing.Pen(blackColor,
20
,
com.mapdigit.drawing.Pen.CAP_BUTT,
com.mapdigit.drawing.Pen.JOIN_MITER, dashArray3,
0
);
graphics2D.drawLine(pen,
40
,
140
,
140
,
140
);
}
}
|