您的位置:首页 > 移动开发 > Android开发

Android游戏Graphics绘图之几何图形绘制

2012-01-15 13:36 441 查看
前两节调整好了画笔,设置好了画布,现在需要在画布上绘制内容了。其实前面我们已经看到了在屏幕上显示的矩形、圆形、三角形等几何图形,下面我们来看看在Android中可以绘制出哪些几何图形,如下表所示。

方法 说明
drawRect绘制矩形
drawCircle绘制圆形
drawOval绘制椭圆
drawPath绘制任意多边形
drawLine绘制直线
drawPoint绘制点
下面我们通过一个示例来看看如何绘制这些几何图形,运行效果如图5-8所示。





示例中分别绘制了空心和实心的几何图形,如下代码所示:

view source

print?

001
package
com.yarin.android.Examples_05_05;
002
 
003
import
android.content.Context;
004
import
android.graphics.Canvas;
005
import
android.graphics.Color;
006
import
android.graphics.Paint;
007
import
android.graphics.Path;
008
import
android.graphics.Rect;
009
import
android.graphics.RectF;
010
import
android.view.KeyEvent;
011
import
android.view.MotionEvent;
012
import
android.view.View;
013
 
014
public
class
GameView
extends

View
implements

Runnable
015
{
016
/** 声明Paint对象 */
017
private

Paint mPaint =
null
;
018
 
019
private

GameView2mGameView2=
null
;
020
public

GameView(Context context)
021
{

022
super
(context);
023
/** 构建对象 */
024
mPaint =
new
Paint();

025
 
026
mGameView2=
new
GameView2(context);
027
 
028
/** 开启线程*/
029
new

Thread(
this
).start();
030
}

031
 
032
public

void
onDraw(Canvas canvas)
033
{

034
super
.onDraw(canvas);
035
 
036
/**设置画布为黑色背景 */
037
canvas.drawColor(Color.BLACK);
038
/** 取消锯齿 */
039
mPaint.setAntiAlias(
true
);
040
 
041
mPaint.setStyle(Paint.Style.STROKE);
042
 
043
{
044
/** 定义矩形对象 */
045
Rect rect1 =
new
Rect();

046
/** 设置矩形大小 */
047
rect1.left =
5
;
048
rect1.top =
5
;
049
rect1.bottom =
25
;
050
rect1.right =
45
;
051
 
052
mPaint.setColor(Color.BLUE);
053
/** 绘制矩形 */
054
canvas.drawRect(rect1,mPaint);
055
 
056
mPaint.setColor(Color.RED);
057
/** 绘制矩形 */
058
canvas.drawRect(
50
,
5
,
90
,
25
,mPaint);

059
 
060
mPaint.setColor(Color.YELLOW);
061
/** 绘制圆形(圆心x,圆心y,半径r,p) */
062
canvas.drawCircle(
40
,
70
,
30
,mPaint);
063
 
064
/** 定义椭圆对象 */
065
RectF rectf1 =
new
RectF();

066
/** 设置椭圆大小 */
067
rectf1.left =
80
;
068
rectf1.top =
30
;
069
rectf1.right =
120
;
070
rectf1.bottom =
70
;
071
 
072
mPaint.setColor(Color.LTGRAY);
073
/** 绘制椭圆 */
074
canvas.drawOval(rectf1,mPaint);
075
 
076
/** 绘制多边形 */
077
Path path1 =
new
Path();

078
 
079
/**设置多边形的点*/
080
path1.moveTo(
150
+
5
,
80
-
50
);
081
path1.lineTo(
150
+
45
,
80
-
50
);
082
path1.lineTo(
150
+
30
,
120
-
50
);
083
path1.lineTo(
150
+
20
,
120
-
50
);
084
/** 使这些点构成封闭的多边形 */
085
path1.close();
086
 
087
mPaint.setColor(Color.GRAY);
088
/** 绘制这个多边形 */
089
canvas.drawPath(path1,mPaint);
090
 
091
mPaint.setColor(Color.RED);
092
mPaint.setStrokeWidth(
3
);
093
/** 绘制直线 */
094
canvas.drawLine(
5
,
110
,
315
,
110
,mPaint);

095
}
096
//
097
//下面绘制实心几何体
098
//
099
mPaint.setStyle(Paint.Style.FILL);
100
{
101
/** 定义矩形对象 */
102
Rect rect1 =
new
Rect();

103
/** 设置矩形大小 */
104
rect1.left =
5
;
105
rect1.top =
130
+
5
;
106
rect1.bottom =
130
+
25
;
107
rect1.right =
45
;
108
 
109
mPaint.setColor(Color.BLUE);
110
/** 绘制矩形 */
111
canvas.drawRect(rect1,mPaint);
112
 
113
mPaint.setColor(Color.RED);
114
/** 绘制矩形 */
115
canvas.drawRect(
50
,
130
+
5
,
90
,
130
+
25
,mPaint);
116
 
117
mPaint.setColor(Color.YELLOW);
118
/** 绘制圆形(圆心x,圆心y,半径r,p) */
119
canvas.drawCircle(
40
,
130
+
70
,
30
,mPaint);

120
 
121
/** 定义椭圆对象 */
122
RectF rectf1 =
new
RectF();

123
/** 设置椭圆大小 */
124
rectf1.left =
80
;
125
rectf1.top =
130
+
30
;
126
rectf1.right =
120
;
127
rectf1.bottom =
130
+
70
;
128
 
129
mPaint.setColor(Color.LTGRAY);
130
/** 绘制椭圆 */
131
canvas.drawOval(rectf1,mPaint);
132
 
133
/** 绘制多边形 */
134
Path path1 =
new
Path();

135
 
136
/**设置多边形的点*/
137
path1.moveTo(
150
+
5
,
130
+
80
-
50
);
138
path1.lineTo(
150
+
45
,
130
+
80
-
50
);
139
path1.lineTo(
150
+
30
,
130
+
120
-
50
);
140
path1.lineTo(
150
+
20
,
130
+
120
-
50
);
141
/** 使这些点构成封闭的多边形 */
142
path1.close();
143
 
144
mPaint.setColor(Color.GRAY);
145
/** 绘制这个多边形 */
146
canvas.drawPath(path1,mPaint);
147
 
148
mPaint.setColor(Color.RED);
149
mPaint.setStrokeWidth(
3
);
150
/** 绘制直线 */
151
canvas.drawLine(
5
,
130
+
110
,
315
,
130
+
110
,mPaint);
152
}
153
 
154
/** 通过ShapeDrawable来绘制几何图形 */
155
mGameView2.DrawShape(canvas);
156
}

157
 
158
// 触笔事件
159
public

boolean
onTouchEvent(MotionEvent event)
160
{

161
return

true
;
162
}

163
 
164
 
165
// 按键按下事件
166
public

boolean
onKeyDown(
int

keyCode,KeyEvent event) 
167
{

168
return

true
;
169
}

170
 
171
 
172
// 按键弹起事件
173
public

boolean
onKeyUp(
int

keyCode,KeyEvent event) 
174
{

175
return

false
;
176
}

177
 
178
 
179
public

boolean
onKeyMultiple(
int

keyCode,
int

repeatCount,KeyEvent event)
180
{

181
return

true
;
182
}

183
 
184
 
185
public

void
run()
186
{

187
while

(!Thread.currentThread().isInterrupted())
188
{
189
try
190
{
191
Thread.sleep(
100
);
192
}
193
catch

(InterruptedException e)
194
{
195
Thread.currentThread().interrupt();
196
}
197
//使用postInvalidate可以直接在线程中更新界面
198
postInvalidate();
199
}
200
}

201
}
当然,在Android中还可以通过ShapeDrawable来绘制图像,ShapeDrawable可以设置面笔的形状。通过getPaint方法可以得到Paint对象,可以像前面一样设置这个画笔的颜色、尺寸等属性。然而,在ShapeDrawable中提供了setBounds方法来设置图形显示的区域,最后通过ShapeDrawable的Draw方法将图形显示到屏幕上,具体实现如下代码所示:

view source

print?

01
package
com.yarin.android.Examples_05_05;
02
 
03
import
android.content.Context;
04
import
android.graphics.Canvas;
05
import
android.graphics.Color;
06
import
android.graphics.Path;
07
import
android.graphics.Rect;
08
import

android.graphics.drawable.ShapeDrawable;
09
import

android.graphics.drawable.shapes.OvalShape;
10
import

android.graphics.drawable.shapes.PathShape;
11
import

android.graphics.drawable.shapes.RectShape;
12
import
android.view.View;
13
 
14
//通过ShapeDrawable来绘制几何图形
15
public
class
GameView2
extends

View
16
{
17
/** 声明ShapeDrawable对象 */
18
ShapeDrawable mShapeDrawable=
null
;
19
 
20
 
21
public

GameView2(Context context)
22
{

23
super
(context);
24
}

25
 
26
public

void
DrawShape(Canvas canvas)
27
{

28
/** 实例化ShapeDrawable对象并说明是绘制一个矩形 */
29
mShapeDrawable =
new
ShapeDrawable(
new

RectShape());
30
 
31
//得到画笔paint对象并设置其颜色
32
mShapeDrawable.getPaint().setColor(Color.RED);
33
 
34
Rect bounds =
new
Rect(
5
,
250
,
55
,
280
);
35
 
36
/** 设置图像显示的区域 */
37
mShapeDrawable.setBounds(bounds);
38
 
39
/** 绘制图像 */
40
mShapeDrawable.draw(canvas);
41
/**=================================*/
42
/** 实例化ShapeDrawable对象并说明是绘制一个椭圆 */
43
mShapeDrawable =
new
ShapeDrawable(
new

OvalShape());
44
 
45
//得到画笔paint对象并设置其颜色
46
mShapeDrawable.getPaint().setColor(Color.GREEN);
47
 
48
/** 设置图像显示的区域 */
49
mShapeDrawable.setBounds(
70
,
250
,
150
,
280
);
50
 
51
/** 绘制图像 */
52
mShapeDrawable.draw(canvas);
53
 
54
Path path1 =
new
Path();

55
/**设置多边形的点*/
56
path1.moveTo(
150
+
5
,
80
+
80
-
50
);
57
path1.lineTo(
150
+
45
,
80
+
80
-
50
);
58
path1.lineTo(
150
+
30
,
80
+
120
-
50
);
59
path1.lineTo(
150
+
20
,
80
+
120
-
50
);
60
/** 使这些点构成封闭的多边形 */
61
path1.close();
62
 
63
//PathShape后面两个参数分别是宽度和高度
64
mShapeDrawable =
new
ShapeDrawable(
new

PathShape(path1,
150
,
150
));
65
 
66
//得到画笔paint对象并设置其颜色
67
mShapeDrawable.getPaint().setColor(Color.BLUE);
68
 
69
/** 设置图像显示的区域 */
70
mShapeDrawable.setBounds(
100
,
170
,
200
,
280
);
71
 
72
/** 绘制图像 */
73
mShapeDrawable.draw(canvas);
74
}

75
}
OK,你现在了解如何在Android中利用Graphics绘制基本的几何图形了吧,谢谢阅读。

文章出处:http://www.itivy.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: