# 安卓五子棋绘制

// 设置画笔的属性
mPaint = new Paint();
mPaint.setStrokeWidth(5);
mPaint.setStyle(Style.STROKE);
mPaint.setAntiAlias(true);

1、获取屏幕的宽和高。private int screen_width, screen_height;
2、设定我们棋盘的单个单元格的宽度（正方形） 屏幕宽/单元格行数 即 lineWidth = screen_width / lineNum;
3、设定棋子的大小 piece_width = lineWidth / 4 * 3; 比棋单元格略小

for (int i = 0; i < lineNum - 1; i++) {
canvas.drawLine(lineWidth, lineWidth * (i + 1), screen_width- lineWidth, lineWidth * (i + 1), mPaint);
}

for (int i = 0; i < lineNum - 1; i++) {
canvas.drawLine(lineWidth * (i + 1), lineWidth,
lineWidth * (i + 1), screen_width - lineWidth, mPaint);
}

float x = event.getX();
float y = event.getY();
canvas.drawBitmap(bitmap_piece, x，y, mPaint);

*lineWidth）为原点去画这个棋子，可是我们需要的棋子的圆心落在两条线的交点处，那么我们就让棋子左移棋子的半径上移他的半径，这样就正好落到了交点。

public class MyView1 extends View {
/** * 默认 如果先手 是 1 那么就是 黑子 先下 /
private int code = 1;
/
* * 画笔 /private Paint mPaint;private int lineNum = 10;
/
* * 记录 是否落子状态 /
private int[][] pieces_state;private int screen_width, screen_height;
private Context context;
/
* * 每行的宽度 /
private int lineWidth;
/
* * 棋子的宽度 /
private int piece_width;
/
* * 存储 棋子的容器 */
private ArrayListlist_pieces;
Bitmap black_piece, white_piece, space_piece;
MyView1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
// 设置画笔的属性mPaint = new Paint();mPaint.setStrokeWidth(5);
mPaint.setStyle(Style.STROKE);
mPaint.setAntiAlias(true);screen_height = ScreenUtils.getScreenHeight(context);screen_width = ScreenUtils.getScreenWidth(context);
lineWidth = screen_width / lineNum;
piece_width = lineWidth / 4 * 3;list_pieces = new ArrayList();
black_piece = BitmapFactory.decodeResource(getResources(),
R.drawable.stone_b2);
space_piece = BitmapFactory.decodeResource(getResources(),
R.drawable.stone_space);
black_piece = Bitmap.createScaledBitmap(black_piece, piece_width,
piece_width, false);
white_piece = BitmapFactory.decodeResource(getResources(),
R.drawable.stone_w2);
white_piece = Bitmap.createScaledBitmap(white_piece, piece_width,
piece_width, false);
space_piece = Bitmap.createScaledBitmap(space_piece, piece_width,
piece_width, false);
pieces_state = new int[lineNum][lineNum];
}
public MyView1(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyView1(Context context) {
this(context, null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 画横线
for (int i = 0; i < lineNum - 1; i++) {
canvas.drawLine(lineWidth, lineWidth * (i + 1), screen_width

• lineWidth, lineWidth * (i + 1), mPaint);
}
// 画竖线
for (int i = 0; i < lineNum - 1; i++) {
canvas.drawLine(lineWidth * (i + 1), lineWidth,
lineWidth * (i + 1), screen_width - lineWidth, mPaint);
}
// 画棋子 可以建一个线程专门用于 刷新
for (int i = 0; i < list_pieces.size(); i++) {
Piece piece = list_pieces.get(i);
if (piece.code == -1) {
canvas.drawBitmap(white_piece, piece.x * lineWidth
• piece_width / 2, piece.y * lineWidth - piece_width
/ 2, mPaint);
} else {
canvas.drawBitmap(black_piece, piece.x * lineWidth
• piece_width / 2, piece.y * lineWidth - piece_width
/ 2, mPaint);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
float x = event.getX();
float y = event.getY();
if (x < lineWidth || x > screen_width - lineWidth / 2
|| y < lineWidth || y > screen_width - lineWidth / 2) {
} else {
Piece point = getRealPoint(code, x, y);
if (point == null) {
} else {
code = -code;
}
}
Vibrator vib = (Vibrator) context
.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(50);
break;
}
invalidate();
return true;
}
public Piece getRealPoint(int code, float x, float y) {
int p_x = (int) (x / lineWidth);
int p_y = (int) (y / lineWidth);
if (pieces_state[p_x][p_y] == 0) {
pieces_state[p_x][p_y] = code;
} else {
return null;
}
Piece piece = new Piece(code, p_x, p_y);
return piece;
}
public boolean isSucces() {
return false;
}
@Override
protected Parcelable onSaveInstanceState() {
// TODO Auto-generated method stub
return super.onSaveInstanceState();
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(state);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width = Math.min(widthSize, heightSize);
setMeasuredDimension(width, width);
}
}