这个例子是绘制多边形,多义形和路径,采用单选钮RadioButton来选择Polys 和Path示例:
UI 设计为 上部分用来显示绘图内容,下部分为两个单选按钮 Polys ,Path。这样layout就和main.xml 不一样,main.xml只含一个com.pstreets.graphics2d.GuidebeeGraphics2DView。因此需在 res\layout下新建一个polys.xml:
android:orientation=”vertical”
android:background=”@drawable/white”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
android:id=”@+id/graphics2dview”
android:layout_weight=”1″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”/>
android:layout_width=”wrap_content”android:layout_height=”wrap_content”
android:orientation=”horizontal”
>
android:layout_width=”wrap_content”
android:orientation=”horizontal”
android:textSize=”20dp”
android:layout_height=”wrap_content”>
android:id=”@+id/radioPolys”
android:layout_width=”wrap_content”
android:textColor=”@color/black”
android:checked=”true”
android:layout_height=”wrap_content”>
android:id=”@+id/radioPath”
android:layout_width=”wrap_content”
android:textColor=”@color/black”
android:layout_height=”wrap_content”>
RadioButton 需包含在RadioGroup中做为一个分组,这里将Polys 设为选中。
定义好Layout资源后,修改 Path.java
privateRadioButton radioPoly;
privateRadioButton radioPath;
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.polys);
graphic2dView
= (GuidebeeGraphics2DView)
findViewById(R.id.graphics2dview);
radioPath = (RadioButton) findViewById(R.id.radioPath);
radioPoly = (RadioButton) findViewById(R.id.radioPolys);
radioPath.setOnClickListener(this);
radioPoly.setOnClickListener(this);
}
应为需要处理按键消息,所以定义了两个RadioButton对象,可以通过findViewById获取实例。因为两个RadioButton这里采用 同样的处理方法,可以让Path实现OnClickListener ,即:public class Path extends Graphics2DActivity implements OnClickListener。完整代码如下:
1publicclassPathextendsGraphics2DActivity
2implementsOnClickListener {
3
4privateRadioButton radioPoly;
5privateRadioButton radioPath;
6
7publicvoidonCreate(Bundle savedInstanceState) {
8super.onCreate(savedInstanceState);
9setContentView(R.layout.polys);
10graphic2dView
11= (GuidebeeGraphics2DView)
12findViewById(R.id.graphics2dview);
13radioPath = (RadioButton) findViewById(R.id.radioPath);
14radioPoly = (RadioButton) findViewById(R.id.radioPolys);
15radioPath.setOnClickListener(this);
16radioPoly.setOnClickListener(this);
17}
18
19@Override
20protectedvoiddrawImage() {
21if(radioPoly.isChecked()) {
22drawPolys();
23}else{
24drawPaths();
25}
26graphic2dView.refreshCanvas();
27
28}
29
30@Override
31publicvoidonClick(View view) {
32drawImage();
33}
34
35privatevoiddrawPaths() {
36AffineTransform mat1;
37
38// The path.
39com.mapdigit.drawing.geometry.Path path;
40
41// Colors
42Color redColor =newColor(0x96ff0000,true);
43Color greenColor =newColor(0xff00ff00);
44Color blueColor =newColor(0x750000ff,true);
45
46String pathdata
47="M 60 20 Q -40 70 60 120 Q 160 70 60 20 z";
48mat1 =newAffineTransform();
49mat1.translate(30,40);
50mat1.rotate(-30* Math.PI /180.0);
51path = com.mapdigit.drawing.geometry.Path.fromString(pathdata);
52// Clear the canvas with white color.
53graphics2D.clear(Color.WHITE);
54
55graphics2D.setAffineTransform(newAffineTransform());
56SolidBrush brush =newSolidBrush(greenColor);
57graphics2D.fill(brush, path);
58graphics2D.setAffineTransform(mat1);
59
60brush =newSolidBrush(blueColor);
61com.mapdigit.drawing.Pen pen
62=newcom.mapdigit.drawing.Pen(redColor,5);
63graphics2D.setPenAndBrush(pen, brush);
64graphics2D.draw(null, path);
65graphics2D.fill(null, path);
66
67}
68
69privatevoiddrawPolys() {
70AffineTransform mat1;
71
72// Colors
73Color redColor =newColor(0x96ff0000,true);
74Color greenColor =newColor(0xff00ff00);
75Color blueColor =newColor(0x750000ff,true);
76
77Polyline polyline;
78Polygon polygon;
79Polygon polygon1;
80
81String pointsdata1
82="59,45,95,63,108,105,82,139,39,140,11,107,19,65";
83mat1 =newAffineTransform();
84mat1.translate(30,40);
85mat1.rotate(-30* Math.PI /180.0);
86polyline =newPolyline();
87polygon =newPolygon();
88polygon1 =newPolygon();
89Point[] points = Point.fromString(pointsdata1);
90for(inti =0; i
91polyline.addPoint(points[i].x, points[i].y);
92polygon.addPoint(points[i].x, points[i].y);
93polygon1.addPoint(points[i].x, points[i].y);
94}
95// Clear the canvas with white color.
96graphics2D.clear(Color.WHITE);
97
98graphics2D.setAffineTransform(newAffineTransform());
99SolidBrush brush =newSolidBrush(greenColor);
100graphics2D.fillPolygon(brush, polygon);
101graphics2D.setAffineTransform(mat1);
102
103brush =newSolidBrush(blueColor);
104com.mapdigit.drawing.Pen pen
105=newcom.mapdigit.drawing.Pen(redColor,5);
106graphics2D.setPenAndBrush(pen, brush);
107graphics2D.fillPolygon(null, polygon1);
108graphics2D.drawPolyline(null, polyline);
109
110}
111
112}
【编辑推荐】
【责任编辑:milk TEL:(010)68476606】
点赞 0