1
/**
2
* <p>Title: Snake</p>
3
* <p>Copyright: (C) 2007 The Android Open Source Project. Licensed under the Apache License, Version 2.0 (the "License")</p>
4
*
@author
Gavin 标注
5
*/
6
7
package
com.deaboway.snake;
8
9
import
android.content.Context;
10
import
android.content.res.TypedArray;
11
import
android.graphics.Bitmap;
12
import
android.graphics.Canvas;
13
import
android.graphics.Paint;
14
import
android.graphics.drawable.Drawable;
15
import
android.util.AttributeSet;
16
import
android.view.View;
17
18
/**
19
* TileView: a View-variant designed for handling arrays of "icons" or other
20
* drawables.
21
*
22
*/
23
//
View 变种,用来处理 一组 贴片—— “icons”或其它可绘制的对象
24
public
class
TileView
extends
View {
25
26
/**
27
* Parameters controlling the size of the tiles and their range within view.
28
* Width/Height are in pixels, and Drawables will be scaled to fit to these
29
* dimensions. X/Y Tile Counts are the number of tiles that will be drawn.
30
*/
31
32
protected
static
int
mTileSize;
33
34
//
X轴的贴片数量
35
protected
static
int
mXTileCount;
36
//
Y轴的贴片数量
37
protected
static
int
mYTileCount;
38
39
//
X偏移量
40
private
static
int
mXOffset;
41
//
Y偏移量
42
private
static
int
mYOffset;
43
44
/**
45
* A hash that maps integer handles specified by the subclasser to the
46
* drawable that will be used for that reference
47
*/
48
//
贴片图像的图像数组
49
private
Bitmap[] mTileArray;
50
51
/**
52
* A two-dimensional array of integers in which the number represents the
53
* index of the tile that should be drawn at that locations
54
*/
55
//
保存每个贴片的索引——二维数组
56
private
int
[][] mTileGrid;
57
58
//
Paint对象(画笔、颜料)
59
private
final
Paint mPaint
=
new
Paint();
60
61
//
构造函数
62
public
TileView(Context context, AttributeSet attrs,
int
defStyle) {
63
super
(context, attrs, defStyle);
64
65
TypedArray a
=
context.obtainStyledAttributes(attrs,
66
R.styleable.TileView);
67
68
mTileSize
=
a.getInt(R.styleable.TileView_tileSize,
12
);
69
70
a.recycle();
71
}
72
73
public
TileView(Context context, AttributeSet attrs) {
74
super
(context, attrs);
75
76
TypedArray a
=
context.obtainStyledAttributes(attrs,
77
R.styleable.TileView);
78
79
mTileSize
=
a.getInt(R.styleable.TileView_tileSize,
12
);
80
81
a.recycle();
82
}
83
84
/**
85
* Rests the internal array of Bitmaps used for drawing tiles, and sets the
86
* maximum index of tiles to be inserted
87
*
88
*
@param
tilecount
89
*/
90
//
设置贴片图片数组
91
public
void
resetTiles(
int
tilecount) {
92
mTileArray
=
new
Bitmap[tilecount];
93
}
94
95
//
回调:当该View的尺寸改变时调用,在onDraw()方法调用之前就会被调用,所以用来设置一些变量的初始值
96
//
在视图大小改变的时候调用,比如说手机由垂直旋转为水平
97
@Override
98
protected
void
onSizeChanged(
int
w,
int
h,
int
oldw,
int
oldh) {
99
100
//
定义X轴贴片数量
101
mXTileCount
=
(
int
) Math.floor(w
/
mTileSize);
102
mYTileCount
=
(
int
) Math.floor(h
/
mTileSize);
103
104
//
X轴偏移量
105
mXOffset
=
((w
-
(mTileSize
*
mXTileCount))
/
2
);
106
107
//
Y轴偏移量
108
mYOffset
=
((h
-
(mTileSize
*
mYTileCount))
/
2
);
109
110
//
定义贴片的二维数组
111
mTileGrid
=
new
int
[mXTileCount][mYTileCount];
112
113
//
清空所有贴片
114
clearTiles();
115
}
116
117
/**
118
* Function to set the specified Drawable as the tile for a particular
119
* integer key.
120
*
121
*
@param
key
122
*
@param
tile
123
*/
124
//
给mTileArray这个Bitmap图片数组设置值
125
public
void
loadTile(
int
key, Drawable tile) {
126
Bitmap bitmap
=
Bitmap.createBitmap(mTileSize, mTileSize,
127
Bitmap.Config.ARGB_8888);
128
Canvas canvas
=
new
Canvas(bitmap);
129
tile.setBounds(
0
,
0
, mTileSize, mTileSize);
130
//
把一个drawable转成一个Bitmap
131
tile.draw(canvas);
132
//
在数组里存入该Bitmap
133
mTileArray[key]
=
bitmap;
134
}
135
136
/**
137
* Resets all tiles to 0 (empty)
138
*
139
*/
140
//
清空所有贴片
141
public
void
clearTiles() {
142
for
(
int
x
=
0
; x
<
mXTileCount; x
++
) {
143
for
(
int
y
=
0
; y
<
mYTileCount; y
++
) {
144
//
全部设置为0
145
setTile(
0
, x, y);
146
}
147
}
148
}
149
150
/**
151
* Used to indicate that a particular tile (set with loadTile and referenced
152
* by an integer) should be drawn at the given x/y coordinates during the
153
* next invalidate/draw cycle.
154
*
155
*
@param
tileindex
156
*
@param
x
157
*
@param
y
158
*/
159
//
给某个贴片位置设置一个状态索引
160
public
void
setTile(
int
tileindex,
int
x,
int
y) {
161
mTileGrid[x][y]
=
tileindex;
162
}
163
164
//
onDraw 在视图需要重画的时候调用,比如说使用invalidate刷新界面上的某个矩形区域
165
@Override
166
public
void
onDraw(Canvas canvas) {
167
168
super
.onDraw(canvas);
169
for
(
int
x
=
0
; x
<
mXTileCount; x
+=
1
) {
170
for
(
int
y
=
0
; y
<
mYTileCount; y
+=
1
) {
171
//
当索引大于零,也就是不空时
172
if
(mTileGrid[x][y]
>
0
) {
173
//
mTileGrid中不为零时画此贴片
174
canvas.drawBitmap(mTileArray[mTileGrid[x][y]], mXOffset
+
x
175
*
mTileSize, mYOffset
+
y
*
mTileSize, mPaint);
176
}
177
}
178
}
179
180
}
181
}